Find the non decreasing order array from given array | Utility function to print the contents of the array ; Function to build array B [ ] ; Lower and upper limits ; To store the required array ; Apply greedy approach ; Print the built array b [ ] ; Driver code
function printArr ( b , n ) { for ( let i = 0 ; i < n ; i ++ ) { document . write ( " " + b [ i ] + " " ) ; } } function ModifiedArray ( a , n ) { let l = 0 , r = Number . MAX_VALUE ; let b = Array ( n ) . fill ( 0 ) ; for ( let i = 0 ; i < n / 2 ; i ++ ) { b [ i ] = Math . max ( l , a [ i ] - r ) ; b [ n - i - 1 ] = a [ i ] - b [ i ] ; l = b [ i ] ; r = b [ n - i - 1 ] ; } printArr ( b , n ) ; } let a = [ 5 , 6 ] ; let n = a . length ; ModifiedArray ( a , 2 * n ) ;