Maximize the rightmost element of an array in k operations in Linear Time | Function to calculate maximum value of Rightmost element ; Initializing ans to store Maximum valued rightmost element ; Calculating maximum value of Rightmost element ; returning rightmost element ; Given Input ; Function Call
function maxRightmostElement ( N , k , p , arr ) { var ans = arr [ N - 1 ] ; for ( var i = N - 2 ; i >= 0 ; i -- ) { var d = Math . min ( arr [ i ] / p , k / ( N - 1 - i ) ) ; k -= d * ( N - 1 - i ) ; ans += d * p ; } return ans ; } var N = 4 , k = 3.5 , p = 2 ; var arr = [ 3 , 8 , 1 , 4 ] ; document . write ( maxRightmostElement ( N , k , p , arr ) ) ;