Interview Question
Software Engineer / DevelopersTeam: Java
Country: India
Interview Type: In-Person
but to mention here please mention that m is not the length of the array but it is m-1 as in that case the array will take the addresses as the elements as you are doing while(i<m) but actually i is only to be run till m-1 that is:
while(i<m-1)
but just to mention you can only call the function with m-1 rather than making changes to the whole function..
.I did not understand 'Merge the two of the arrays and start placing in the destination array at index "m", thus leaving space for "m" number of elements at the beginning of the array' . Your solution is as good as merging arrays a[ ],b[ ] into d[ ] and then merging d[] with c[ ] . Please let me know if I am wrong
Use 4 pointers from the ends of the 3 sorted arrays as well as the larger array.
1. Use 3 way comparison to find out the maximum element of the 3 sorted arrays and place it at the end of the larger array. Decrement the pointer of the larger array and of the sorted array where the maximum was found.
2. Repeat step 1 until one of the array is exhausted.
3. Start doing 2-way comparison and placing the maximum value in the larger array
4. Repeat step 3 until only one array remains
5. Copy the contents of the remaining sorted array into the larger array.
Nyc... I also thought of the same solution. I have a doubt though.
Will this solution or the one suggested by - ashot madatyan will give less number of comparisons?
I think the number of comparisons in both the cases is the same. In order for an element to be placed at it's correct position in the larger array 3 comparisons are required in both the cases.
public int[] threeWayMerge(int[] a, int[] b, int[] c) {
if (a == null || b == null || c == null) {
return null;
}
int[] res = new int[a.length + b.length + c.length];
System.arraycopy(a, 0, res, 0, a.length);
twoWayMerge(res, a.length, b);
twoWayMerge(res, a.length + b.length, c);
return res;
}
public void twoWayMerge(int[] large, int count, int[] small) {
int p1 = count - 1;
int p2 = small.length - 1;
int last = count + small.length - 1;
while (p1 >= 0 && p2 >= 0) {
large[last--] = large[p1] > small[p2] ? large[p1--] : small[p2--];
}
if (p2 >= 0) {
System.arraycopy(small, 0, large, 0, p2 + 1);
}
}
@ashot madatyan :.I did not understand 'Merge the two of the arrays and start placing in the destination array at index "m", thus leaving space for "m" number of elements at the beginning of the array' . Your solution is as good as merging arrays a[ ],b[ ] into d[ ] and then merging d[] with c[ ] . Please let me know if I am wrong
Suppose you have the following inputs:
a[]
b[]
c[]
d[] // destination array of size 3*m
m // the size of each small array
1. Merge the two of the arrays and start placing in the destination array at index "m",
thus leaving space for "m" number of elements at the beginning of the array.
2. Now start merging the third array with the larger array in the destination array
located at position "m"
- ashot madatyan July 01, 2013