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 ; Print the answer ; Given N , K & M
function maxValueAtIndexK ( N , K , M ) { let S1 = 0 , S2 = 0 ; S1 = K * ( K + 1 ) / 2 ; S2 = ( N - K - 1 ) * ( N - K ) / 2 ; let X = ( M + S1 + S2 ) / N ; document . write ( X ) ; } let N = 3 , K = 1 , M = 7 ; maxValueAtIndexK ( N , K , M ) ;