Maximize the rightmost element of an array in k operations in Linear Time | Function to calculate maximum value of Rightmost element ; Calculating maximum value of Rightmost element ; Checking if arr [ i ] is operationable ; Performing operation of i - th element ; Decreasing the value of k by 1 ; Printing rightmost element ; Given Input ; Function Call
def maxRightmostElement ( N , k , p , arr ) : NEW_LINE INDENT while ( k ) : NEW_LINE INDENT for i in range ( N - 2 , - 1 , - 1 ) : NEW_LINE INDENT if ( arr [ i ] >= p ) : NEW_LINE INDENT arr [ i ] = arr [ i ] - p NEW_LINE arr [ i + 1 ] = arr [ i + 1 ] + p NEW_LINE break NEW_LINE DEDENT DEDENT k = k - 1 NEW_LINE DEDENT print ( arr [ N - 1 ] ) NEW_LINE DEDENT N = 4 NEW_LINE p = 2 NEW_LINE k = 5 NEW_LINE arr = [ 3 , 8 , 1 , 4 ] NEW_LINE maxRightmostElement ( N , k , p , arr ) NEW_LINE