Minimum increments of Non | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getMinOps ( int arr [ ] , int n ) { int ans = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { ans += max ( arr [ i ] - arr [ i + 1 ] , 0 ) ; } return ans ; } int main ( ) { int arr [ ] = { 1 , 3 , 1 , 2 , 4 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << ( getMinOps ( arr , n ) ) ; }