Maximize element at index K in an array with at most sum M and difference between adjacent elements at most 1 | Function to calculate the maximum possible value at index K ; Stores the sum of elements in the left and right of index K ; Stores the maximum possible value at index K ; Prthe answer ; Driver Code ; Given N , K & M
def maxValueAtIndexK ( N , K , M ) : NEW_LINE INDENT S1 = 0 ; S2 = 0 ; NEW_LINE S1 = K * ( K + 1 ) // 2 ; NEW_LINE S2 = ( N - K - 1 ) * ( N - K ) // 2 ; NEW_LINE X = ( M + S1 + S2 ) // N ; NEW_LINE print ( X ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 3 ; K = 1 ; M = 7 ; NEW_LINE maxValueAtIndexK ( N , K , M ) ; NEW_LINE DEDENT