Efficiently merging two sorted arrays with O ( 1 ) extra space |  ; Find maximum element of both array ; increment by one to avoid collision of 0 and maximum element of array in modulo operation ; recover back original element to compare ; update element by adding multiplication with new number ; update element by adding multiplication with new number ; process those elements which are left in array a ; process those elements which are left in array b ; finally update elements by dividing with maximum element ; finally update elements by dividing with maximum element ; Driver Code ; Length of a ; length of b ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void mergeArray ( int a [ ] , int b [ ] , int n , int m ) { int mx = 0 ; for ( int i = 0 ; i < n ; i ++ ) { mx = max ( mx , a [ i ] ) ; } for ( int i = 0 ; i < m ; i ++ ) { mx = max ( mx , b [ i ] ) ; } mx ++ ; int i = 0 , j = 0 , k = 0 ; while ( i < n && j < m && k < ( n + m ) ) { int e1 = a [ i ] % mx ; int e2 = b [ j ] % mx ; if ( e1 <= e2 ) { if ( k < n ) a [ k ] += ( e1 * mx ) ; else b [ k - n ] += ( e1 * mx ) ; i ++ ; k ++ ; } else { if ( k < n ) a [ k ] += ( e2 * mx ) ; else b [ k - n ] += ( e2 * mx ) ; j ++ ; k ++ ; } } while ( i < n ) { int el = a [ i ] % mx ; if ( k < n ) a [ k ] += ( el * mx ) ; else b [ k - n ] += ( el * mx ) ; i ++ ; k ++ ; } while ( j < m ) { int el = b [ j ] % mx ; if ( k < n ) a [ k ] += ( el * mx ) ; else b [ k - n ] += ( el * mx ) ; j ++ ; k ++ ; } for ( int i = 0 ; i < n ; i ++ ) a [ i ] = a [ i ] / mx ; for ( int i = 0 ; i < m ; i ++ ) b [ i ] = b [ i ] / mx ; return ; } int main ( ) { int a [ ] = { 3 , 5 , 6 , 8 , 12 } ; int b [ ] = { 1 , 4 , 9 , 13 } ; int n = sizeof ( a ) / sizeof ( int ) ; int m = sizeof ( b ) / sizeof ( int ) ; mergeArray ( a , b , n , m ) ; cout << " First ▁ array ▁ : ▁ " ; for ( int i = 0 ; i < n ; i ++ ) cout << a [ i ] << " ▁ " ; cout << endl ; cout << " Second ▁ array ▁ : ▁ " ; for ( int i = 0 ; i < m ; i ++ ) cout << b [ i ] << " ▁ " ; cout << endl ; return 0 ; }