Merge Without Extra Space | Practice | GeeksforGeeks

Given two sorted arrays arr1[] of size N and arr2[] of size M. Each array is sorted in non-decreasing order. Merge the two arrays into one sorted array in non-decreasing order without using any extra space.

Example 1:
Input:
N = 4, M = 5
arr1[] = {1, 3, 5, 7}
arr2[] = {0, 2, 6, 8, 9}
Output: 0 1 2 3 5 6 7 8 9
Explanation: Since you can't use any 
extra space, modify the given arrays
to form 
arr1[] = {0, 1, 2, 3}
arr2[] = {5, 6, 7, 8, 9}


Example 2:
Input:
N = 2, M = 3
arr1[] = {10, 12}
arr2[] = {5, 18, 20}
Output: 5 10 12 18 20
Explanation: Since you can't use any
extra space, modify the given arrays
to form 
arr1[] = {5, 10}
arr2[] = {12, 18, 20}
 
Your Task:
You don't need to read input or print anything. Complete the function merge() which takes the two arrays arr1[], arr2[] and their sizes n and m, as input parameters. The function does not return anything. Use the given arrays to sort and merge arr1[] and arr2[] in-place. 
Note: The generated output will print all the elements of arr1[] followed by all the elements of arr2[].

Expected Time Complexity: O((n+m)*log(n+m))
Expected Auxiliary Space: O(1)

Constraints:
1 <= N, M <= 5*104
0 <= arr1i, arr2i <= 106
 
0
shashankchauhansc75 days ago
    def merge(self, arr1, arr2, n, m):        i = n-1       j = 0       while i >= 0 and j < m:
           if arr1[i] > arr2[j]:               arr1[i], arr2[j] = arr2[j], arr1[i]
           else:               break
           i -= 1           j += 1
       arr1.sort()       arr2.sort()
0
anmolbansal25 days ago
public void merge(int arr1[], int arr2[], int n, int m) {       // code here       int ans[]=new int[n+m];      for(int i=0;i<n;i++){          ans[i]=arr1[i];      }      for(int i=n,j=0;i<m+n;i++){          ans[i]=arr2[j++];      }      Arrays.sort(ans);      for(int i=0;i<n;i++){          arr1[i]=ans[i];      }      for(int i=0,j=n;i<m;i++){          arr2[i]=ans[j++];      }   }
+1
2019kucp10855 days ago
	void merge(int arr1[], int 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
	        break;
	    }
	    sort(arr1,arr1+n);
	    sort(arr2,arr2+m);
	}
-1
jy9041476 days ago
java solution jitu yadav
 public void merge(int arr1[], int arr2[], int n, int m) {            int ans[]=new int[n+m];       for(int i=0;i<n;i++){           ans[i]=arr1[i];       }       for(int i=n,j=0;i<m+n;i++){           ans[i]=arr2[j++];       }       Arrays.sort(ans);       for(int i=0;i<n;i++){           arr1[i]=ans[i];       }       for(int i=0,j=n;i<m;i++){           arr2[i]=ans[j++];       }                       }
+1
aishaparween5461 week ago
  public void merge(int arr1[], int arr2[], int n, int m) {       // code here          int i=n-1,j=0;      while(i>=0 && j<m){       if(arr1[i]<arr2[j]){           break;       } else{       int temp=arr1[i];       arr1[i]=arr2[j];       arr2[j]=temp;       i--;       j++;   }   }  Arrays.sort(arr1);Arrays.sort(arr2);
0
rajsaurabh9011 week ago
 void merge(int arr1[], int arr2[], int n, int m) {    // code here        int p2;    int a=(n+m);    while(a>1){        int p1=0;        a=(a+1)/2;        p2=a;                while(p2<n+m){            if(p1<n && p2<n){                if(arr1[p1]<arr1[p2]){                    p1++;p2++;                }                else{                    swap(arr1[p1],arr1[p2]);                    p1++;p2++;                }            }            else if(p1<n && p2>n-1){                int b=p2-n;                if(arr1[p1]<arr2[b]){                    p1++;p2++;                }                else{                    swap(arr1[p1],arr2[b]);                    p1++;p2++;                }            }            else if(p1>n-1 && p2>n-1){                int b2=p2-n;                int b1=p1-n;                if(arr2[b1]<arr2[b2]){                    p1++;p2++;                }                else{                    swap(arr2[b1],arr2[b2]);                    p1++;p2++;                }            }        }    }}};
-5
jeetsorathiya1 week ago
easy solution :
 
class Solution {
   public void merge(int arr1[], int arr2[], int n, int m) {       // code here       int ans[] = new int[n+m];              for(int i=0;i<n;i++)       {           ans[i]=arr1[i];       }              for(int i=n,j=0;i<m+n;i++)       {           ans[i]=arr2[j++];       }              Arrays.sort(ans);       for(int i=0;i<n;i++)       {           arr1[i]=ans[i];       }       for(int i=0,j=n;i<m;i++)       {           arr2[i]=ans[j++];       }   }}
0
21f10050132 weeks ago
Running successfully in Jupyter Notebook , with same input jupyter ans GforG compiler shows different output.
 
def merge(self,arr1,arr2, n, m): 
        # code here
        i,j=0,0
        while i<n and j<m:
            #print(i,j)
            if arr1[i]<arr2[j]:
                i+=1
                #j+=1
            elif arr1[i]==arr2[j]:
                i+=1
                j+=1
            else:
                (arr1[i],arr2[j])=(arr2[j],arr1[i])
                i+=1
                #j+=1
            arr2=sorted(arr2)
        return

-1
aniket752 weeks ago
 public void merge(int arr1[], int arr2[], int n, int m) {      // use merge sort concept       int merge[] = new int[n+m];       int i = 0;       int j=0;       int k=0;              while(i<n && j<m){           if(arr1[i]<arr2[j]){               merge[k] = arr1[i];               i++;               k++;           }           else{               merge[k] = arr2[j];               j++;               k++;           }       }       while(i<n){           merge[k] = arr1[i];           i++;           k++;       }       while(j<m){           merge[k] = arr2[j];           j++;           k++;       }       // this loop use to avoid extra space for(i=0;i<n;i++){     arr1[i]=merge[i]; } for(i=0,j=n;i<m;i++){     arr2[i]=merge[j++]; }              }
-1
yedurusrinivasreddy99882 weeks ago
Simple Python Code
def merge(self, arr1, arr2, n, m): 
       # code here
       res=[]
       for i in arr1:
           res.append(i)
       for j in arr2:
           res.append(j)
       res.sort()
       arr1.clear()
       arr2.clear()
       for i in range(n):
           arr1.append(res[i])
       for j in range(n,m+n):
           arr2.append(res[j])
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.