Maximize sum of K corner elements in Array | Function to return maximum sum ; Initialize variables ; Iterate over first K elements of array and update the value for curr_points ; Update value for max_points ; j points to the end of the array ; Return the final result ; Driver code
def maxPointCount ( arr , K , size ) : NEW_LINE INDENT curr_points = 0 NEW_LINE max_points = 0 NEW_LINE for i in range ( K ) : NEW_LINE INDENT curr_points += arr [ i ] NEW_LINE DEDENT max_points = curr_points NEW_LINE j = size - 1 NEW_LINE for i in range ( K - 1 , - 1 , - 1 ) : NEW_LINE INDENT curr_points = ( curr_points + arr [ j ] - arr [ i ] ) NEW_LINE max_points = max ( curr_points , max_points ) NEW_LINE j -= 1 NEW_LINE DEDENT return max_points NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 8 , 4 , 4 , 8 , 12 , 3 , 2 , 9 ] NEW_LINE K = 3 NEW_LINE n = len ( arr ) NEW_LINE print ( maxPointCount ( arr , K , n ) ) NEW_LINE DEDENT