Maximize the smallest array element by incrementing all elements in a K | Java 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
class GFG { static long n , m , k , l , r , i ; static boolean check ( long v , long [ ] a ) { long tec = 0 , ans = 0 ; long [ ] b = new long [ ( int ) ( n + k + 1 ) ] ; for ( int i = 0 ; i < n ; i ++ ) { tec -= b [ i ] ; if ( a [ i ] + tec < v ) { long mov = v - a [ i ] - tec ; ans = ans + mov ; tec += mov ; b [ i + ( int ) k ] = mov ; } } return ans <= m ; } static long FindLargest ( long [ ] a ) { l = 1 ; r = ( long ) Math . pow ( 10 , 10 ) ; while ( r - l > 0 ) { long tm = ( l + r + 1 ) / 2 ; if ( check ( tm , a ) ) l = tm ; else r = tm - 1 ; } return l ; } public static void main ( String [ ] args ) { long [ ] a = { 2 , 2 , 2 , 2 , 1 , 1 } ; m = 2 ; k = 3 ; n = a . length ; System . out . println ( FindLargest ( a ) ) ; } }