Given an array of integers, reverse every maximal strictly ascending subarray - java

How would I rearrange the elements of a given array of integers in place so that the elements of every maximal strictly ascending subarray are reversed?
For example, given the array { 5, 7, 10, 4, 2, 7, 8, 1, 3 }, after executing this method, the elements of the array would be { 10, 7, 5, 4, 8, 7, 2, 3, 1 }.
My attempt only sorts the integers in descending order. How can I make these nested loops identify the maximal strictly ascending subarrays? I believe the outer loop should have to repeatedly find the end of the current ascending sequence, while the inner loop reverses the subarray up to that point.
public class MyClass {
public static void main(String args[]) {
int[] arr = {5, 7, 10, 4, 2, 7, 8, 1, 3};
for (int i=0; i<arr.length-1; i++) {
if (arr[i] < arr[i+1]) {
int t = arr[i+1];
arr[i+1] = arr[i];
arr[i] = t;
}
for (int j=0; j<arr.length-1; j++) {
if (arr[j] < arr[j+1]) {
int t = arr[j+1];
arr[j+1] = arr[j];
arr[j] = t;
}
}
}
String result = Arrays.toString(arr);
System.out.println(result); // [10, 8, 7, 7, 5, 4, 3, 2, 1]
}
}

I see why you are using nested loop. But I think you'll need to keep track of start and end instead of just swapping. Here's how I solved it using a stack:
public static void main(String[] args) {
System.out.println(Arrays.toString(reverseAscendingSubArray(new int[]{5, 7, 10, 4, 2, 7, 8, 1, 3})));
}
private static int[] reverseAscendingSubArray(int[] arr) {
Stack<Integer> stack = new Stack<>();
int[] result = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
if (i == 0 || arr[i - 1] < arr[i]) {
stack.push(arr[i]);
} else {
for (int j = stack.size(); j > 0; j--) {
result[i - j] = stack.pop();
}
stack.push(arr[i]);
}
}
if (!stack.empty()) {
for (int j = stack.size(); j > 0; j--) {
result[arr.length - j] = stack.pop();
}
}
return result;
}
Output
[10, 7, 5, 4, 8, 7, 2, 3, 1]
Explanation
I keep pushing elements into the stack until the current element is greater than the previous one. As soon as I get something smaller than the previous, I pop all elements from the stack and write them to a new array. This pushing and popping will reverse the elements.

You can try using bubble sort as in the code below.
public static void reverseAscendingSubarrays(int[] items){
int start = -1;
int stop = -1;
for (int i = 0; i < items.length; i++){
if (i != items.length - 1){
if (items[i] <= items[i + 1]){
if (start == -1) {
start = i;
}
}
else {
if (start != -1) {
stop = i;
}
}
}
else{
if (start != -1){
stop = i;
}
}
if (start != -1 && stop != -1){
//sort array from start to stop (uses bubble sort - inefficient for large arrays)
for (int n = 0; n < stop - start + 1; n++)
for (int j = start; j < stop; j++){
if (items[j] < items[j+1]){
//swap
int temp = items[j];
items[j] =items[j+1];
items[j+1] = temp;
}
}
start = -1;
stop = -1;
}
}
}

Related

Move all zeroes in a given array to the end and replace each non-zero element with the closest greater value, if any

Looking for the optimised solution for the below problem.
Given an unsorted array, we are required to move all zeroes to the end of the array and at same time find the next closest greater number of each element(non-zero) and return the same element incase if there is no next greater element for an element in the array .
Input = {6,1,5,0,0,3,8,6,4}
Output = {8,3,6,4,8,6,4,0,0}
I tried the below :
public class next_closest_element {
public static void main(String[] arg) {
int[] input = {6, 1, 5, 0, 0, 3, 8, 6, 4};
Stack<Integer> stack = new Stack<Integer>();
int k = 0;
int count = 0;
int last_index_value =input.length-1;
for (int i = 0; i < input.length; i++) {
if (input[i] != 0) {
int j = i + 1;
boolean flag = false;
while (j < input.length && input[i] != 0) {
if (input[j] > input[i]) {
if (stack.empty() || !flag) {
stack.push(input[j]);
flag = true;
j++;
} else if (stack.peek() > input[j]) {
stack.pop();
stack.push(input[j]);
flag = true;
j++;
} else {
j++;
}
} else {
j++;
}
}
if (flag) {
input[k] = stack.peek();
k++;
}
else {
input[k]=input[i];
k++;
}
}
else{
count +=1;
}
}
while(count>0){
input[last_index_value]=0;
last_index_value--;
count--;
}
for (int s :
input) {
System.out.println(s);
}
}
}
First shoveling the zeroes to the right would be one optimisation.
Possibly replacing with next closest greater element I have interpreted as next following element (as you seemed to do, as otherwise the last 4 might have become the overwritten 5).
static int[] f(int[] values) {
// In-situ (in-place) algorithm.
int n = 0; // The count of non-zero values.
for (int i = 0; i < values.length; ++i) {
if (values[i] != 0) {
values[n] = values[i];
++n;
}
}
// Zero the rest:
// (With a second array the remaining values from n upwards would
// already be zero.)
for (int i = n; i < values.length; ++i) {
values[i] = 0;
}
// {6, 1, 5, 0, 0, 3, 8, 6, 4} -> {6, 1, 5, 3, 8, 6, 4, [n] 0, 0}
// (First optimisation.) Now we only need to deal with n non-zero values.
// Search the next (A) closest greatest (B) number, when found substitute.
// Unoptimized:
for (int i = 0; i < n; ++i) {
int ithValue = values[i];
boolean hasClosest = false;
int closest = Integer.MAX_VALUE;
for (int j = i + 1; j < n; ++j) {
int jthValue = values[j];
if (jthValue > ithValue && (!hasClosest || jthValue < closest)) {
closest = jthValue;
hasClosest = true;
values[i] = jthValue;
}
}
}
// {8, 3, 6, 4, 8, 6, 4, 0, 0}
return values;
}
public static void main(String[] args) {
int[] input = {6, 1, 5, 0, 0, 3, 8, 6, 4};
System.out.println(Arrays.toString(f(input)));
}
The last piece is not well optimized.
An other interpretation of "closest greatest:"
int[] sorted = Arrays.copyOf(values, n);
Arrays.sort(sorted);
for (int i = 0; i < n; ++i) {
int j = Arrays.binarySearch(sorted, values[i] + 1);
if (j < 0) { // Not found 1 greater.
j = ~j; // Even greater.
}
if (j < n) {
values[i] = sorted[j];
}
}
// {8, 3, 6, 4, 8, 8, 5, 0, 0}
Sorting cost O(N log N), as does the loop O(N) times binary search O(log N).
So it is faster, costing memory. But that is comparible with you stack usage.
By the way, a cleaned up version of your code could have been put on CodeReview.

JAVA/Selection sort : the console shows different results from what I've entered

I'm learning how to implement selection sort.
What I expected from the code is an ascending order output, like: {1,3,4,5,6,7,9}
But the console showed: 9, 4, 1, 6, 5, 3, 7, 9, 7, 1, 4, 5, 3, 6, 9, 7, 6, 1, 4, 3, 5, 9, 7, 6, 5, 1, 3, 4, 9, 7, 6, 5, 4, 1, 3, 9, 7, 6, 5, 4, 3, 1
What am I supposed to change to get the correct result?
This is my code:
public static void main(String[] args) {
int[]arr = {4,6,1,9,5,3,7};
for(int i = 0; i<arr.length-1; i++) {
for(int j = i+1; j<arr.length; j++) {
if(arr[i]<arr[j]) {
int a=arr[i];
arr[i]=arr[j];
arr[j]=a;
}
}
for (int b = 0; b <arr.length; b++) {
System.out.print(arr[b] + ", ");
}
}
You are printing the array while you are doing the sorting. Instead, print the array when you are done with the sorting -
public static void main(String[] args) {
int[] arr = {4, 6, 1, 9, 5, 3, 7};
for (int i = 0; i < arr.length - 1; i++) { // first loop
for (int j = i + 1; j < arr.length; j++) { // nested loop
if (arr[i] < arr[j]) { // if condition
int a = arr[i];
arr[i] = arr[j];
arr[j] = a;
} // if condition ends
} // nested loop ends
} // first loop ends
// Now the array is sorted, it's good to print.
for (int b = 0; b < arr.length; b++) {
System.out.print(arr[b] + ", ");
}
}
There is one more catch. Though your sorting would work, your implementation is not really selection sort. It's bubble sort implementation. The key difference is, in selection sort you need to find the min value and place it in the ith position for every i. So you only swap once per iteration. In bubble sort, we swap repeatedly like you did.
Also, as pointed out in the comment, for ascending order, you have to flip to condition for swapping. So the correct implementation would be -
public static void main(String[] args) {
int[] arr = {4, 6, 1, 9, 5, 3, 7};
for (int i = 0; i < arr.length - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < arr.length; j++) {
if (arr[minIndex] > arr[j]) {
minIndex = j;
}
}
int tmp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = tmp;
}
for (int b = 0; b < arr.length; b++) {
System.out.print(arr[b] + ", ");
}
}
There is an easier way with Arrays.sort(arr)
public static void main(String[] args) {
int[] arr = {4,6,1,9,5,3,7};
Arrays.sort(arr);
System.out.print(Arrays.toString(arr));
}
}
Selection Sort - java code
class selectionsort{
public static void sort(int[] arr){
int temp,min;
for(int i=0;i<arr.length;i++)
{
min=i;
for(int j=i+1;j<arr.length;j++)
{
if(arr[min]>arr[j])
{
min=j;
}
}
temp=arr[min];
arr[min]=arr[i];
arr[i]=temp;
}
}
public static void main(String [] args){
int ar[]={4,6,1,9,5,3,7};
sort(ar);
System.out.print("After sort :");
for(int j=0;j<ar.length;j++){
System.out.print(ar[j]+" ");
}
}
}

Duplicate zero in array by modifying the array in place

There is a fixed length array arr of integers, duplicate each occurrence of zero, shifting the remaining elements to the right. The elements beyond the length of the original array are not written.
We have to modify input array in place and doesn't have to create new array.
So I created that but it is duplicating the zero which is at the end of array and not the previous zeros. Can somebody help me with this?
public static void addPos() {
int arr[] = { 1, 2, 0, 3, 0, 5, 0, 7, 8 };
int result[] = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
if (arr[i] == 0) {
int loc = i;
for (int j = 0; j < loc; j++) {
result[j] = arr[j];
result[loc] = 0;
}
for (int j = loc + 1; j < arr.length; j++) {
result[j] = arr[j - 1];
}
}
}
for (int k = 0; k < arr.length; k++)
System.out.println(result[k]);
}
Output
1
2
0
3
0
5
0
0
7
Expected output:
1
2
0
0
3
0
0
5
0
Every iteration of the loop overwrites the results from the previous iteration, so the end result only shows the results from the last iteration, which duplicates the last 0 is duplicated.
One way to solve this is by iterating backwards "right to left". It simplifies a lot of things. You can get rid of the auxiliary result array. The basic idea is, go backwards in the array, and every time you find a 0, you duplicate it by rewriting the array to the right of the zero.
public static void addPos() {
int arr[] = {1, 2, 0, 3, 0, 5, 0, 7, 8};
for (int i = arr.length - 1; i >= 0; i--) {
if (arr[i] == 0) {
// duplicate it!
for (int j = arr.length - 1; j > i; j--) {
arr[j] = arr[j-1];
}
}
}
for (int k = 0; k < arr.length; k++) {
System.out.println(arr[k]);
}
}
The for loop keeps overwriting the values in result array, hence the result shows only last duplication.You should not be using the result array at all.Keep shipting values in the original array itself.
You can refer to below code.
for(int i=0;i<arr.length-1;i++){
if(arr[i]==0){
for(int j=arr.length-1;j>i;j--){
arr[j]=arr[j-1];
}
i++;
}
}
public void duplicateZeros(int[] arr)
{
int i=0;
while(i<arr.length)
{
if(arr[i]==0)
{
int j=arr.length-1;
while(j != i)
{
arr[j]=arr[j-1];
j--;
}
i=i+2;
}
else
{
i=i+1;
}
}
}
Without using any other Array.
class Solution {
public void duplicateZeros(int[] arr) {
for(int i=0;i<arr.length;i++){
if(arr[i]==0){
for(int j=arr.length-1;j>i;j--){
arr[j]=arr[j-1];
}
i=i+1;
}
}
}
}
So one has this:
int[] arr = { 1, 2, 0, 3, 0, 5, 0, 7, 8 };
public static void duplicateZeros(int[] arr) {
and should get
{ 1, 2, 0, 3, 0, 5, 0, 7, 8 }
v___
{ 1, 2, 0, 0, 3, 0, 5, 0, 7 }
v___
{ 1, 2, 0, 0, 3, 0, 0, 5, 0 }
This looks like:
for (int i = 1; i < n; ++i) {
if (arr[i - 1] == 0) {
insert at i a 0;
}
}
insert at i a 0:
// First move the remaining to the right: i .. n-2
...
// Then fill in the zero
arr[i] = 0;
Python solution for anyone interested adapted from here
the solution is non-trivial if you do not separate the action of the pointer iterating over the list and the insertions. It's very easy to write a for-loop that adds 0's ad-infinitum.
def duplicateZeros(arr):
# define the incrementor
i = 0
# loop through all dynamic elements
while i < len(arr)-1:
# if the character is a zero
if arr[i]==0:
# remove the last item from the array
arr.pop()
# insert a zero in front of current element
arr.insert(i+1, 0)
# move one place forward
i += 1
# increment to the next character
i += 1
Solution 1: Loop from start to end. If zero is found, move the elements from next index and fill the next as zero and skip next.
public static void duplicateZeros(int[] arr) {
System.out.println("BEGIN duplicateZeros:" + Arrays.toString(arr));
for(int i=0; i<arr.length-1; ++i) {
if (arr[i] == 0) {
move(arr, i);
++i;
}
}
System.out.println("END duplicateZeros:" + Arrays.toString(arr) +"\n");
}
private static void move(int[] arr, int index) {
// move to the right from index+1
for(int i=arr.length-1; i>index; i--) {
arr[i] = arr[i-1];
}
// fill 0 at index
arr[index] = 0 ;
}
Solution2: Loop from end to start. If zero is found, move the elements from next index and fill the current index as zero.
public static void duplicateZeros(int[] arr) {
System.out.println("BEGIN duplicateZeros:" + Arrays.toString(arr));
for(int i=arr.length-1; i>=0; i--) {
if (arr[i] == 0) {
move(arr, i);
}
}
System.out.println("END duplicateZeros:" + Arrays.toString(arr) +"\n");
}
private static void move(int[] arr, int index) {
// move to the right from index+1
for(int i=arr.length-1; i>index; i--) {
arr[i] = arr[i-1];
}
// fill 0 at index
arr[index] = 0 ;
}
class Solution:
def duplicateZeros(self, arr: List[int]) -> None:
"""
Do not return anything, modify arr in-place instead.
"""
if len(arr)==0:
return arr
index = 0
while index < len(arr):
print(index,end=" ")
if arr[index]==0:
arr.insert(index+1,0)
arr.pop()
index+=1
index+=1

Java: how to count number of non repeated elements in an integer array?

How do you count the number of numbers that occur in only one of the two arrays?
Example: countdifference([2, 4, 4, 4, 4, 6, 6, 8], [3, 4, 6, 6, 9]) returns 4 because 4 and 6 are duplicates, remaining numbers are 2, 8, 3 and 9.
noOfRepeatsCount method is intended to account for repeated elements in arrayB: i.e. 6
I am getting an index array out of bounds exception: -1 for the noOfRepeatsCount method. Any ideas why?
public class countNonRepeated {
static int countDifference(int[] arrayA, int[] arrayB) {
int count = 0, repeatCount = 0, noOfRepeatsCount = 0;
/*
int noOfRepeats = 0;
for (int k = 0; k < arrayB.length; k++) {
if (arrayB[k] == arrayB[k - 1]) {
noOfRepeats++;
} else {
continue;
}
*/
for (int i = 0; i < arrayA.length; i++) {
for (int j = 0; j < arrayB.length; j++) {
if (arrayA[i] == arrayB[j]) {
if (arrayA[i + 1] == arrayB[j]) {
repeatCount++;
} else {
count++;
}
} else {
continue;
}
}
}
int length = arrayA.length + arrayB.length;
return length - (noOfRepeatsCount * repeatCount) - (count * 2);
}
static int noOfRepeatsCount(int[] arrayB) {
int noOfRepeats = 0;
for (int k = 0; k < arrayB.length; k++) {
if (arrayB[k] == arrayB[k - 1]) {
noOfRepeats++;
} else {
continue;
}
}
return noOfRepeats;
}
public static void main(String args[]) {
int arrayA[] = { 2, 4, 4, 4, 4, 6, 6, 8 };
int arrayB[] = { 3, 4, 6, 6, 9 };
// System.out.println(noOfRepeatsCount(arrayA));
System.out.println(countDifference(arrayA, arrayB));
}
}
See the for loop in noOfRepeatsCount method. if (arrayB[k] == arrayB[k - 1]) makes exception error. Because initial k value is 0, you access arrayB[-1].
If you want to solve this problem, change for (int k = 0; k < arrayB.length; k++) to for (int k = 1; k < arrayB.length; k++).
What about converting the arrays to Sets and using a combination of removeAll(), addAll() and size() to get the desired result. I guess that this would make the algorithm clearer and avoid off-by-one errors.
I have no time right now to post a code sample, sorry.

Java - Merge Two Arrays without Duplicates (No libraries allowed)

Need assistance with programming issue.
Must be in Java. Cannot use any libraries (Arraylist, etc.).
int[] a = {1, 2, 3, 4, 8, 5, 7, 9, 6, 0}
int[] b = {0, 2, 11, 12, 5, 6, 8}
Have to create an object referencing these two arrays in a method that merges them together, removes duplicates, and sorts them.
Here's my sort so far. Having difficulty combining two arrays and removing duplicates though.
int lastPos;
int index;
int temp;
for(lastPos = a.length - 1; lastPos >= 0; lastPos--) {
for(index = 0; index <= lastPos - 1; index++) {
if(a[index] > a[index+1]) {
temp = a[index];
a[index] = a[index+1];
a[index+1] = temp;
}
}
}
a method that merges them together, removes duplicates, and sorts them.
I suggest you break this down into helper methods (and slightly tweak the order of operations). Step 1, merge the two arrays. Something like,
static int[] mergeArrays(int[] a, int[] b) {
int[] c = new int[a.length + b.length];
for (int i = 0; i < a.length; i++) {
c[i] = a[i];
}
for (int i = 0; i < b.length; i++) {
c[a.length + i] = b[i];
}
return c;
}
Step 2, sort the new array (your existing sort algorithm is fine). Like,
static void sortArray(int[] a) {
for (int lastPos = a.length - 1; lastPos >= 0; lastPos--) {
for (int index = 0; index <= lastPos - 1; index++) {
if (a[index] > a[index + 1]) {
int temp = a[index];
a[index] = a[index + 1];
a[index + 1] = temp;
}
}
}
}
Finally, remove duplicates. Step 3a, count unique values. Assume they're unique, and decrement by counting adjacent (and equal) values. Like,
static int countUniqueValues(int[] c) {
int unique = c.length;
for (int i = 0; i < c.length; i++) {
while (i + 1 < c.length && c[i] == c[i + 1]) {
i++;
unique--;
}
}
return unique;
}
Then step 3b, take the unique count and build your result with the previous methods. Like,
public static int[] mergeDedupSort(int[] a, int[] b) {
int[] c = mergeArrays(a, b);
sortArray(c);
int unique = countUniqueValues(c);
int[] d = new int[unique];
int p = 0;
for (int i = 0; i < c.length; i++) {
d[p++] = c[i];
while (i + 1 < c.length && c[i] == c[i + 1]) {
i++;
}
}
return d;
}
Then you can test it with your arrays like
public static void main(String[] args) {
int[] a = { 1, 2, 3, 4, 8, 5, 7, 9, 6, 0 };
int[] b = { 0, 2, 11, 12, 5, 6, 8 };
int[] c = mergeDedupSort(a, b);
System.out.println(Arrays.toString(c));
}
And I get
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12]
Merge Two Arrays without Duplicates and Sort it (No libraries used).
Using an object.
public class MergeRemoveDupSort {
public int[] mergeRemoveDupSortIt(int[] a, int[] b) {
int [] c = mergeIt(a,b);
int [] d = removeIt(c);
int [] e = sortIt(d);
return e;
}
private int[] mergeIt(int[] a, int[] b) {
int[] c = new int[a.length + b.length];
int k=0;
for (int n : a) c[k++]=n;
for (int n : b) c[k++]=n;
return c;
}
private int[] removeIt(int[] c) {
int len=c.length;
for (int i=0;i<len-1;i++)
for (int j=i+1;j<len;j++)
if (c[i] == c[j]) {
for (int k=j;k<len-1;k++)
c[k]=c[k+1];
--len;
}
int [] r = new int[len];
for (int i=0;i<r.length;i++)
r[i]=c[i];
return r;
}
private int[] sortIt(int[] a) {
for(int index=0; index<a.length-1; index++)
for(int i=index+1; i<a.length; i++)
if(a[index] > a[i]){
int temp = a[index];
a[index] = a[i];
a[i] = temp;
}
return a;
}
public void printIt(int[] a) {
System.out.print("[");
for (int i=0;i<a.length;i++){
System.out.print(a[i]);
if (i!=a.length-1) System.out.print(",");
else System.out.print("]");
}
}
public static void main(String[] args) {
int[] a = {1, 2, 3, 4, 8, 5, 7, 9, 6, 0};
int[] b = {0, 2, 11, 12, 5, 6, 8};
MergeRemoveDupSort array = new MergeRemoveDupSort();
int [] r = array.mergeRemoveDupSortIt(a, b);
array.printIt(r);
}
}
You should use IntStream like this.
int[] a = {1, 2, 3, 4, 8, 5, 7, 9, 6, 0};
int[] b = {0, 2, 11, 12, 5, 6, 8};
int[] merged = IntStream
.concat(IntStream.of(a), IntStream.of(b))
.distinct()
.sorted()
.toArray();
System.out.println(Arrays.toString(merged));
Assuming that array a and array b are sorted, the following code will merge them into a third array merged_array without duplicates :
public static int[] get_merged_array(int[] a, int[] b, int a_size, int b_size)
{
int[] merged_array = new int[a_size + b_size];
int i = 0 , j = 0, x = -1;
for(; i < a_size && j < b_size;)
{
if(a[i] <= b[j])
{
merged_array[++x] = a[i];
++i;
}
else
{
if(merged_array[x] != b[j])
{
merged_array[++x] = b[j]; // avoid duplicates
}
++j;
}
}
--i; --j;
while(++i < a_size)
{
merged_array[++x] = a[i];
}
while(++j < b_size)
{
merged_array[++x] = b[j];
}
return merged_array;
}
Hope this may help, all the best :)
try{
int[] a = {1, 2, 3, 4, 8, 5, 7, 9, 6, 0};
int[] b = {0, 2, 11, 12, 5, 6, 8};
int[] c = new int[a.length+b.length];
int[] final = new int[a.length+b.length];
int i = 0;
for(int j : final){
final[i++] = -1;
}
i = 0;
for(int j : a){
c[i++] = j;
}
for(int j : b){
c[i++] = j;
}
boolean check = false;
for(int j = 0,k = 0; j < c.length; j++){
for(int l : fin){
if( l == c[j] )
check = true;
}
if(!check){
final[k++] = c[j];
} else check = false;
}
} catch(Exception ex){
ex.printStackTrace();
}
I prefer you to use Hashset for this cause it never allow duplicates
and there is another method in java 8 for arraylist to remove duplicates
after copying all elements to c follow this code
List<Integer> d = array.asList(c);
List<Integer> final = d.Stream().distinct().collect(Collectors.toList());
final.forEach(System.out::println());
This code is lot much better than previous one and you can again transform final to array like this
int array[] = new int[final.size()];
for(int j =0;j<final.size();j++){
array[j] = final.get(j);
}
Hope my work will be helpful .
Let me restate your question.
You want a program that takes two arbitrary arrays, merges them removing any duplicates, and sorts the result.
First of all, if you have access to any data structure, there are much better ways of doing this. Ideally, you would use something like a TreeSet.
However, assuming all you have access to is arrays, your options are much more limited. I'm going to go ahead and assume that each of the two arrays initially has no duplicates.
Let's assume the first array is of length m and the second array is of length n.
int[] array1; // length m
int[] array2; // length n
First, let's sort both arrays.
Arrays.sort(array1);
Arrays.sort(array2);
This assumes you have access to the Arrays class which is part of the standard Java Collections Framework. If not, any reasonable merge implementation (like MergeSort) will do the trick.
The sorts will take O(n log n + m log m) with most good sort implementations.
Next, let's merge the two sorted arrays. First, we need to allocate a new array big enough to hold all the elements.
int[] array3 = new int[size];
Now, we will need to insert the elements of array1 and array2 in order, taking care not to insert any duplicates.
int index=0, next=0, i=0, j=0;
int last = Integer.MAX_INT;
while(i < m || j < n) {
if(i == m)
next = array2[j++];
else if(j == n)
next = array1[i++];
else if(array1[i] <= array2[j])
next = array1[i++];
else
next = array2[j++];
if(last == next)
continue;
array3[index++] = next;
}
Now, you have your array. Just one problem - it could have invalid elements at the end. One last copy should take care of it...
int[] result = Arrays.copyOf(array3, index + 1);
The inserts and the final copy will take O(n + m), so the overall efficiency of the algorithm should be O(n log n + m log n).

Categories