Maximize the smallest array element by incrementing all elements in a K | Javascript program for above approach ; Function to check if the smallest value of v is achievable or not ; Create array to store previous moves ; Remove previous moves ; Add balance to ans ; Update contiguous subarray of length k ; Number of moves should not exceed m ; Function to find the maximum value of the smallest array element that can be obtained ; Perform Binary search ; Given Input ; Function Call
let n = 0 , m = 0 , k = 0 , l = 0 , r = 0 , i = 0 ; function check ( v , a ) { let tec = 0 , ans = 0 ; let b = new Array ( n + k + 1 ) . fill ( 0 ) ; for ( i = 0 ; i < n ; i ++ ) { tec -= b [ i ] ; if ( a [ i ] + tec < v ) { let mov = v - a [ i ] - tec ; ans = ans + mov ; tec += mov ; b [ i + k ] = mov ; } } return ans <= m ; } function FindLargest ( a ) { l = 1 ; r = Math . pow ( 10 , 10 ) ; while ( r - l > 0 ) { let tm = Math . floor ( ( l + r + 1 ) / 2 ) ; if ( check ( tm , a ) ) l = tm ; else r = tm - 1 ; } return l ; } let a = [ 2 , 2 , 2 , 2 , 1 , 1 ] ; m = 2 ; k = 3 ; n = a . length ; document . write ( FindLargest ( a ) ) ;