Minimum number of operations required to make an array non | 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 ; Given input ; Function call
function countMinSteps ( arr , N ) { let val = 0 ; let mx = Number . MIN_SAFE_INTEGER ; for ( let i = 0 ; i < N ; i ++ ) { let curr = arr [ i ] ; mx = Math . max ( mx , curr ) ; val = Math . max ( val , mx - curr ) ; } let res = 0 ; while ( ( 1 << res ) - 1 < val ) { ++ res ; } return res ; } let arr = [ 1 , 7 , 6 , 5 ] ; let N = arr . length ; document . write ( countMinSteps ( arr , N ) ) ;