Maximize sum of K corner elements in Array | Function to return maximum sum ; Base case ; Pick the start index ; Pick the end index ; Recursive function call ; Return the final answer ; Function to find the maximized sum ; Driver code
def maxSum ( arr , K , start , end , max_sum ) : NEW_LINE INDENT if ( K == 0 ) : NEW_LINE INDENT return max_sum NEW_LINE DEDENT max_sum_start = max_sum + arr [ start ] NEW_LINE max_sum_end = max_sum + arr [ end ] NEW_LINE ans = max ( maxSum ( arr , K - 1 , start + 1 , end , max_sum_start ) , maxSum ( arr , K - 1 , start , end - 1 , max_sum_end ) ) NEW_LINE return ans NEW_LINE DEDENT def maximizeSum ( arr , K , n ) : NEW_LINE INDENT max_sum = 0 NEW_LINE start = 0 NEW_LINE end = n - 1 NEW_LINE print ( maxSum ( arr , K , start , end , max_sum ) ) 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 maximizeSum ( arr , K , n ) NEW_LINE DEDENT