Reduce the array by deleting elements which are greater than all elements to its left | Function to implement merging of arr [ ] ; Function to delete all elements which satisfy the condition A [ i ] > A [ i - 1 ] ; Divide array into its subarray ; Getting back merged array with all its right element greater than left one . ; Given array arr [ ]
function merge ( x , y ) { for ( let i = 0 ; i < y . length ; i ++ ) { if ( x [ x . length - 1 ] > y [ i ] ) x . push ( y [ i ] ) ; } return x ; } function mergeDel ( l ) { if ( l . length == 1 ) return l ; let m = Math . floor ( l . length / 2 ) ; let temp1 = l . slice ( 0 , m ) ; let temp2 = l . slice ( m , l . length ) ; return merge ( mergeDel ( temp1 ) , mergeDel ( temp2 ) ) ; } let arr = [ 5 , 4 , 3 , 2 , 1 ] ; let ans = mergeDel ( arr ) ; document . write ( " " ) ; document . write ( ans . join ( " " ) ) ; document . write ( " " ) ;