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
function maxSum ( arr , K , start , end , max_sum ) { if ( K == 0 ) return max_sum ; let max_sum_start = max_sum + arr [ start ] ; let max_sum_end = max_sum + arr [ end ] ; let ans = Math . max ( maxSum ( arr , K - 1 , start + 1 , end , max_sum_start ) , maxSum ( arr , K - 1 , start , end - 1 , max_sum_end ) ) ; return ans ; } function maximizeSum ( arr , K , n ) { let max_sum = 0 ; let start = 0 ; let end = n - 1 ; document . write ( maxSum ( arr , K , start , end , max_sum ) ) ; } let arr = [ 8 , 4 , 4 , 8 , 12 , 3 , 2 , 9 ] ; let K = 3 ; let n = arr . length ; maximizeSum ( arr , K , n ) ;