Maximize first array element by performing given operations at most K times | Function to maximize the first array element ; Traverse the array ; Initialize cur_val to a [ i ] ; If all operations are not over yet ; If current value is greater than zero ; Incrementing first element of array by 1 ; Decrementing current value of array by 1 ; Decrementing number of operations by i ; If current value is zero , then break ; Print first array element ; Given array ; Size of the array ; Given K ; Prints the maximum possible value of the first array element
function getMax ( arr , N , K ) { for ( i = 1 ; i < N ; i ++ ) { var cur_val = arr [ i ] ; while ( K >= i ) { if ( cur_val > 0 ) { arr [ 0 ] = arr [ 0 ] + 1 ; cur_val = cur_val - 1 ; K = K - i ; } else break ; } } document . write ( arr [ 0 ] ) ; } var arr = [ 1 , 0 , 3 , 2 ] ; var N = arr . length ; var K = 5 ; getMax ( arr , N , K ) ;