Merge two sorted arrays with O ( 1 ) extra space | CPP program for the above approach ; Function to merge two arrays ; Untill i less than equal to k or j is less tha m ; Sort first array ; Sort second array ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void merge ( int arr1 [ ] , int arr2 [ ] , int n , int m ) { int i = 0 , j = 0 , k = n - 1 ; while ( i <= k and j < m ) { if ( arr1 [ i ] < arr2 [ j ] ) i ++ ; else { swap ( arr2 [ j ++ ] , arr1 [ k -- ] ) ; } } sort ( arr1 , arr1 + n ) ; sort ( arr2 , arr2 + m ) ; } int main ( ) { int ar1 [ ] = { 1 , 5 , 9 , 10 , 15 , 20 } ; int ar2 [ ] = { 2 , 3 , 8 , 13 } ; int m = sizeof ( ar1 ) / sizeof ( ar1 [ 0 ] ) ; int n = sizeof ( ar2 ) / sizeof ( ar2 [ 0 ] ) ; merge ( ar1 , ar2 , m , n ) ; cout << " After ▁ Merging ▁ STRNEWLINE First ▁ Array : ▁ " ; for ( int i = 0 ; i < m ; i ++ ) cout << ar1 [ i ] << " ▁ " ; cout << " Second Array : " for ( int i = 0 ; i < n ; i ++ ) cout << ar2 [ i ] << " ▁ " ; return 0 ; }