Merge Without Extra Space | Practice | GeeksforGeeks

Given two sorted arrays arr1[] and arr2[] of sizes n and m in non-decreasing order. Merge them in sorted order without using any extra space. Modify arr1 so that it contains the first N elements and modify arr2 so that it contains the last M elements. 
 
Example 1:
Input: 
n = 4, arr1[] = [1 3 5 7] 
m = 5, arr2[] = [0 2 6 8 9]
Output: 
arr1[] = [0 1 2 3]
arr2[] = [5 6 7 8 9]
Explanation:
After merging the two 
non-decreasing arrays, we get, 
0 1 2 3 5 6 7 8 9.
Example 2:
Input: 
n = 2, arr1[] = [10, 12] 
m = 3, arr2[] = [5 18 20]
Output: 
arr1[] = [5 10]
arr2[] = [12 18 20]
Explanation:
After merging two sorted arrays 
we get 5 10 12 18 20.



Your Task:
You don't need to read input or print anything. You only need to complete the function merge() that takes arr1, arr2, n and m as input parameters and modifies them in-place so that they look like the sorted merged array when concatenated.
 
Expected Time Complexity:  O((n+m) log(n+m))
Expected Auxilliary Space: O(1)
 
Constraints:
1 <= n, m <= 5*104
0 <= arr1i, arr2i <= 107
0
aditya1923it1006in 4 hours
GAP / Shell Sort Solution
 
class Solution
{
    //Function to merge the arrays.
    public static void merge(long arr1[], long arr2[], int n, int m) 
    {
        // code here
        int gap =(int) Math.ceil((double)(n + m) / 2.0);
        
        while(gap>0){
            int i = 0, j = gap;
            while(j<(n+m)){
                if(j<n && arr1[i] > arr1[j]){
                    arr1[i] = arr1[i] ^ arr1[j];
                    arr1[j] = arr1[i] ^ arr1[j];
                    arr1[i] = arr1[i] ^ arr1[j];
                }
                if(j>=n && i<n && arr1[i] > arr2[j-n]){
                    arr1[i] = arr1[i] ^ arr2[j-n];
                    arr2[j-n] = arr1[i] ^ arr2[j-n];
                    arr1[i] = arr1[i] ^ arr2[j-n];
                }
                if(j>=n && i>=n && arr2[i-n] > arr2[j-n]){
                    arr2[i-n] = arr2[i-n] ^ arr2[j-n];
                    arr2[j-n] = arr2[i-n] ^ arr2[j-n];
                    arr2[i-n] = arr2[i-n] ^ arr2[j-n];
                }
                j++;
                i++;
            }
            if(gap == 1){
                gap = 0;
            }
            else gap = (int) Math.ceil((double) (gap) / 2.0);;
        }
    }
}

0
himanshuharsh19994 hours ago
			int i = n-1, j = 0;
            while(i>=0 && j<m){
                if (arr1[i] > arr2[j]){
                    swap(arr1[i], arr2[j]);
                    i--;
                    j++;
                }
                else break;
            }
            sort(arr1, arr1+n);
            sort(arr2, arr2+m);
0
abhinayabhi1415 hours ago
 int firstLastIndex = n-1;           int secondStartIndex = 0;           long long temp = -1;           for(int i=0; i<min(m,n); i++)           {               if(arr1[firstLastIndex] > arr2[secondStartIndex])                   swap(arr1[firstLastIndex] , arr2[secondStartIndex]);               firstLastIndex--;               secondStartIndex++;           }           sort(arr1,arr1+n);           sort(arr2,arr2+m);
+1
16julyaayush1 day ago
Gap Method
TC - O(nlogn)
SC - O(1)
 
int nextGap(int gap){           if(gap <= 1){               return 0;           }                      return (gap/2 + gap%2);       }       void merge(long long arr1[], long long arr2[], int n, int m)        {            int gap = (n+m)/2;           while(gap > 0){               // fist array               int i = 0;               for(;i+gap < n; i++){                   if(arr1[i] > arr1[i+gap]){                       swap(arr1[i], arr1[i+gap]);                   }               }               // comparint in both array               int j = 0;               if(gap > n){                   j = gap-n;               }               for(; i < n and j < m; i++, j++){                   if(arr1[i] > arr2[j]){                       swap(arr1[i], arr2[j]);                   }               }               // comparing second array               if(j < m){                   for(j = 0 ;j + gap < m; j++){                       if(arr2[j] > arr2[j+gap]){                           swap(arr2[j], arr2[j+gap]);                       }                   }               }                              gap = nextGap(gap);           }       } 
 
+1
nikhilgoyal1451 day ago
class Solution{   public:       //Function to merge the arrays.       void merge(long long arr1[], long long arr2[], int n, int m)        {            int firstLastIndex = n-1;           int secondStartIndex = 0;           long long temp = -1;           for(int i=0; i<min(m,n); i++)           {               if(arr1[firstLastIndex] > arr2[secondStartIndex])                   swap(arr1[firstLastIndex] , arr2[secondStartIndex]);               firstLastIndex--;               secondStartIndex++;           }           sort(arr1,arr1+n);           sort(arr2,arr2+m);       } }; 
0
ashivamsahu143 days ago
JAVA Solution 
class Solution
{
   //Function to merge the arrays.
   public static void merge(long arr1[], long arr2[], int n, int m) 
   {
       // code here 
         int i=0, j=0, k = n-1;
         while(i <= k && j < m){
             if(arr1[i] < arr2[j]){
                 i++;
             }
             else{
                 long temp = arr2[j];
                 arr2[j] = arr1[k];
                 arr1[k] = temp;
                 j++;
                 k--;
                 
             }
         }
         Arrays.sort(arr1);
         Arrays.sort(arr2);
   }
}
+3
ashivamsahu143 days ago
CPP Solution
for(long long first=n-1,second=0;first>=0&&second<m;first--,second++)
           {
               if(arr1[first]>=arr2[second])
               {
                   swap(arr1[first],arr2[second]);
               }
               else
               break;
               
           }
           sort(arr1,arr1+n);
           sort(arr2,arr2+m);
       } 
0
naveen15iysn4 days ago
void merge(int *arr1, int *arr2, int n, int m) {
       
        // code here
        int i,k,j=0;
        for(i=0;i<n;i++)
           {
               if(arr1[i]>arr2[j])
               {
               int temp=arr1[i];
                arr1[i]=arr2[j];
               arr2[j]=temp;
                k=j;
                while((k+1<m)&&(arr2[k]>arr2[k+1]))
               {
                int temp=arr2[k+1];
                arr2[k+1]=arr2[k];
                arr2[k]=temp;
                ++k;
                 }
                }
           }
                               
    }
0
sachinsingh5864 days ago
// EASY C++ SOLUTION
 
void merge(long long arr1[], long long arr2[], int n, int m)        {            // code here            for(long long first=n-1,second=0;first>=0&&second<m;first--,second++)           {               if(arr1[first]>=arr2[second])               {                   swap(arr1[first],arr2[second]);               }               else               break;                          }           sort(arr1,arr1+n);           sort(arr2,arr2+m);       } 
0
raunakmishra12434 days ago
 void swap(long long &x, long long &y)
        {
            long long t;
            t=x;
            x=y;
            y=t;
        }
        void merge(long long arr1[], long long arr2[], int n, int m) 
        { 
           
             int i=n-1;
             int j=0;
             while(i>=0 && j<m)
             {
                 if(arr1[i]>arr2[j])
                 {
                     swap(arr1[i],arr2[j]);
                     i--;
                     j++;
                 }
                 else
                 {
                     i--;
                 }
             }
             sort(arr1,arr1+n);
             sort(arr2,arr2+m);
    
        } 
        
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested 
                                    against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code.
                                    On submission, your code is tested against multiple test cases consisting of all
                                        possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as
                                        the final solution code.
You can view the solutions submitted by other users from the submission tab.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems does not guarantee the 
                                correctness of code. On submission, your code is tested against multiple test cases
                                    consisting of all possible corner cases and stress constraints.