// Program to merge two sorted arrays without any extra space in Java /* Author : Suraj Kumar Modi Github : https://github.com/skmodi649 */ /** Algorithm : * 1) Initialize i,j as n-1,0 where n is size of arr1 * 2) Iterate through every element of arr1 and arr2 using two pointers i and j respectively * while(i>=0 && j=0 && j arr2[j]) { int temp = arr1[i]; arr1[i] = arr2[j]; arr2[j] = temp; } i--; j++; } Arrays.sort(arr1); Arrays.sort(arr2); } } /** TEST CASES : * * Test Case 1 : * Input : n = 3 , m = 4 * arr1[] = {1,27,36} arr2[] = {12,14,16,22} * Output : * 1 12 14 16 22 27 36 * * Test Case 2 : * Input : n = 4 , m = 5 * arr1[] = {0,12,21,27} arr2[] = {4,6,8,10,12} * Output : * 0 4 6 8 10 12 12 21 27 */ /** * Time Complexity : O((n+m)log(n+m)) * Auxiliary Space Complexity : O(1) */