Efficiently merging two sorted arrays with O ( 1 ) extra space |  ; Length of first arr1 ; Length of second arr2 ; Now traverse the array1 and if arr2 first element is less than arr1 then swap ; Swap ; We will store the firstElement of array2 and left shift all the element and store the firstElement in arr2 [ k - 1 ] ; Read the arr1 ; Read the arr2 ; Driver Code
using System ; class GFG { public static void mergeArray ( int [ ] arr1 , int [ ] arr2 ) { int n = arr1 . Length ; int m = arr2 . Length ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr1 [ i ] > arr2 [ 0 ] ) { int temp = arr1 [ i ] ; arr1 [ i ] = arr2 [ 0 ] ; arr2 [ 0 ] = temp ; int firstElement = arr2 [ 0 ] ; int k ; for ( k = 1 ; k < m && arr2 [ k ] < firstElement ; k ++ ) { arr2 [ k - 1 ] = arr2 [ k ] ; } arr2 [ k - 1 ] = firstElement ; } } foreach ( int i in arr1 ) { Console . Write ( i + " ▁ " ) ; } Console . WriteLine ( ) ; foreach ( int i in arr2 ) { Console . Write ( i + " ▁ " ) ; } } static public void Main ( ) { int [ ] arr1 = { 1 , 3 , 5 , 7 } ; int [ ] arr2 = { 0 , 2 , 6 , 8 , 9 } ; mergeArray ( arr1 , arr2 ) ; } }