@@ -0,0 +1,1720 @@
+package ds.arrays;%0A%0Aimport java.util.Arrays;%0A%0A/**%0A * Copy/merge array1 of size 'm' to an array2 of size 'm+n' %0A * Array1 : contains only m elements %0A * array2 : contains n elements stored in array of size 'm+n'%0A * %0A * O/p : sorted arr2 with copies contents of array1%0A * %0A * array1 : 1,3,4,6  (m=4)%0A * array2 : 4, -1, 5,-8, -1, -1, -1 (m+n = 7), -1 indicates empty place holders%0A * %0A * %0A * Note : assuming arrays have positive integers only, no dups%0A * %0A * @author bboggaramrama%0A *%0A */%0Apublic class ArrayMerger %7B%0A%0A%09public static void main(String args%5B%5D) %7B%0A%09%09ArrayMerger driver = new ArrayMerger();%0A%09%09driver.copyArrays(new int%5B%5D %7B5,7,9,25%7D,new int%5B%5D %7B2,8,-1,-1,-1,13,-1,15,20%7D);%0A%09%09%0A%09%09// Tests%0A%09%7D%0A%09%0A%09/**%0A%09 * 1. Move all the elements in array2 to end%0A%09 * 2. Then start merge step from array1 and array2 (from end), Keep adding the smallest to start of array2. thats it !! %0A%09 */%0A%09public void copyArrays(int%5B%5D arr1, int%5B%5D arr2) %7B%0A%09%09%0A%09%09// Move all the elements in array2 to end. Keep two pointers i and j, Start decrementing them over copying on non -1's%0A%09%09int m = arr1.length;%0A%09%09int n = arr2.length;%0A%09%09int i = n-1, j = n-1;%0A%09%09while(i %3E= 0) %7B%0A%09%09%09if(arr2%5Bi%5D != -1) %7B%0A%09%09%09%09arr2%5Bj%5D = arr2%5Bi%5D;%0A%09%09%09%09i--;%0A%09%09%09%09j--;%0A%09%09%09%7D else %7B%0A%09%09%09%09i--;%0A%09%09%09%7D%0A%09%09%7D%0A%09%09%0A%09// Lets start the  merge step and comparing, copying resply%0A%09 i = 0; // Start of first array%0A%09 j = n - m -1 ; // start of second array%0A%09 int k = 0;%0A%09%09while (k %3C n-1 && j%3Cn ) %7B%0A%09%09%09int element1 = arr1%5Bi%5D;%0A%09%09%09int element2 = arr2%5Bj%5D;%0A%0A%09%09%09if (element1 %3E element2) %7B%0A%09%09%09%09arr2%5Bk%5D = element2;%0A%09%09%09%09j++;%0A%09%09%09%7D else %7B%0A%09%09%09%09arr2%5Bk%5D = element1;%0A%09%09%09%09i++;%0A%09%09%09%7D%0A%0A%09%09%09k++; // Increment the k to keep track of where we are at copying the%0A%09%09%09%09%09// arr2%0A%09%09%7D%0A%09%09System.out.println(Arrays.toString(arr2));%0A%09%7D%0A%09%0A%7D%0A

Copy contents from one array (m size) to another array (m+n size)