Minimize count of increments of each element of subarrays required to make Array non | Java program for the above approach ; Function to find the minimum number of operations required to make the array non - increasing ; Stores the count of required operations ; If arr [ i ] > arr [ i + 1 ] , no increments required . Otherwise , add their difference to the answer ; Return the result res ; Driver Code
import java . io . * ; import java . util . * ; class GFG { public static int getMinOps ( int [ ] arr ) { int res = 0 ; for ( int i = 0 ; i < arr . length - 1 ; i ++ ) { res += Math . max ( arr [ i + 1 ] - arr [ i ] , 0 ) ; } return res ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 3 , 4 , 1 , 2 } ; System . out . println ( getMinOps ( arr ) ) ; } }