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 ; Driver Code ; Given array ; Size of the array ; Given K ; Prints the maximum possible value of the first array element
def getMax ( arr , N , K ) : NEW_LINE INDENT for i in range ( 1 , N , 1 ) : NEW_LINE INDENT cur_val = arr [ i ] NEW_LINE while ( K >= i ) : NEW_LINE INDENT if ( cur_val > 0 ) : NEW_LINE INDENT arr [ 0 ] = arr [ 0 ] + 1 NEW_LINE cur_val = cur_val - 1 NEW_LINE K = K - i NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT DEDENT print ( arr [ 0 ] ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 0 , 3 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE K = 5 NEW_LINE getMax ( arr , N , K ) NEW_LINE DEDENT