Minimum increments of Non | Java Program to implement the above approach ; 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
import java . io . * ; import java . util . * ; class GFG { public static int getMinOps ( int [ ] arr ) { int ans = 0 ; for ( int i = 0 ; i < arr . length - 1 ; i ++ ) { ans += Math . max ( arr [ i ] - arr [ i + 1 ] , 0 ) ; } return ans ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 3 , 1 , 2 , 4 } ; System . out . println ( getMinOps ( arr ) ) ; } }