Maximize the smallest array element by incrementing all elements in a K | Python 3 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 ; Driver Code ; Given Input ; Function Call
n = 0 NEW_LINE m = 0 NEW_LINE k = 0 NEW_LINE l = 0 NEW_LINE r = 0 NEW_LINE i = 0 NEW_LINE from math import pow NEW_LINE def check ( v , a ) : NEW_LINE INDENT tec = 0 NEW_LINE ans = 0 NEW_LINE b = [ 0 for i in range ( n + k + 1 ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT tec -= b [ i ] NEW_LINE if ( a [ i ] + tec < v ) : NEW_LINE INDENT mov = v - a [ i ] - tec NEW_LINE ans = ans + mov NEW_LINE tec += mov NEW_LINE b [ i + k ] = mov NEW_LINE DEDENT DEDENT return ( ans <= m ) NEW_LINE DEDENT def FindLargest ( a ) : NEW_LINE INDENT l = 1 NEW_LINE r = pow ( 10 , 10 ) NEW_LINE while ( r - l > 0 ) : NEW_LINE INDENT tm = ( l + r + 1 ) // 2 NEW_LINE if ( check ( tm , a ) ) : NEW_LINE INDENT l = tm NEW_LINE DEDENT else : NEW_LINE INDENT r = tm - 1 NEW_LINE DEDENT DEDENT return l NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = [ 2 , 2 , 2 , 2 , 1 , 1 ] NEW_LINE m = 2 NEW_LINE k = 3 NEW_LINE n = len ( a ) NEW_LINE print ( int ( FindLargest ( a ) ) ) NEW_LINE DEDENT