Minimum number of operations required to make an array non | C ++ program for the above approach ; Function to count the minimum number of steps required to make arr non - decreasing ; Stores differences ; Stores the max number ; Traverse the array arr [ ] ; Update mx ; Update val ; Stores the result ; Iterate until 2 ^ res - 1 is less than val ; Return the answer ; Driver Code ; Given input ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countMinSteps ( int arr [ ] , int N ) { int val = 0 ; int mx = INT_MIN ; for ( int i = 0 ; i < N ; i ++ ) { int curr = arr [ i ] ; mx = max ( mx , curr ) ; val = max ( val , mx - curr ) ; } long long res = 0 ; while ( ( 1LL << res ) - 1 < val ) { ++ res ; } return res ; } int main ( ) { int arr [ ] = { 1 , 7 , 6 , 5 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countMinSteps ( arr , N ) ; return 0 ; }