Minimum increments of Non | Function to return to the minimum number of operations required to make the array non - decreasing ; Stores the count of operations ; If arr [ i ] > arr [ i + 1 ] , add arr [ i ] - arr [ i + 1 ] to the answer Otherwise , add 0 ; Driver Code
function getMinOps ( arr ) { let ans = 0 ; for ( let i = 0 ; i < arr . length - 1 ; i ++ ) { ans += Math . max ( arr [ i ] - arr [ i + 1 ] , 0 ) ; } return ans ; } let arr = [ 1 , 3 , 1 , 2 , 4 ] ; document . write ( getMinOps ( arr ) ) ;