Maximize sum of K corner elements in Array

11 Jul, 2022
Given an array arr[] and an integer K, the task is to find the maximize the sum of K elements in the Array by taking only corner elements.
A corner element is an element from the start of the array or from the end of the array.
Examples:  
Input: arr[] = {8, 4, 4, 8, 12, 3, 2, 9}, K = 3 Output: 21 Explanation: The optimal strategy is to pick the elements form the array is, two indexes from the beginning and one index from the end. All other possible choice will yield lesser sum. Hence, arr[0] + arr[1] + arr[7] = 21.
Input: arr[] = {2, 1, 14, 6, 4, 3}, K = 3 Output: 17 Explanation: We will get the maximum sum by picking first three elements form the array. Hence, Optimal choice is: arr[0] + arr[1] + arr[2] = 17  
Naive Approach: The idea is to use Recursion. As we can only take a start or end index value hence initialize two variables and take exactly K steps and return the maximum sum among all the possible combinations. The recursive approach has exponential complexity due to its overlapping subproblem and optimal substructure property. 
Below is the implementation of the above approach:  
C++
C
Java
Python3
C#
Javascript
// C++ program to maximize the sum of K elements// in the array by taking only corner elements #include <bits/stdc++.h>using namespace std; // Function to return maximum sumint maxSum(int arr[], int K, int start, int end, int max_sum){    // Base case    if (K == 0)        return max_sum;     // Pick the start index    int max_sum_start = max_sum + arr[start];     // Pick the end index    int max_sum_end = max_sum + arr[end];     // Recursive function call    int ans = max(        maxSum(arr, K - 1, start + 1, end, max_sum_start),        maxSum(arr, K - 1, start, end - 1, max_sum_end));     // Return the final answer    return ans;} // Function to find the maximized sumvoid maximizeSum(int arr[], int K, int n){    int max_sum = 0;    int start = 0;    int end = n - 1;    cout << maxSum(arr, K, start, end, max_sum);} // Driver codeint main(){    int arr[] = { 8, 4, 4, 8, 12, 3, 2, 9 };    int K = 3;    int n = sizeof(arr) / sizeof(arr[0]);    maximizeSum(arr, K, n);    return 0;} // This code is contributed by Aditya Kumar (adityakumar129)
// C++ program to maximize the sum of K elements// in the array by taking only corner elements #include <stdio.h> // Find maximum between two numbers.int max(int num1, int num2){    return (num1 > num2 ) ? num1 : num2;} // Function to return maximum sumint maxSum(int arr[], int K, int start, int end, int max_sum){    // Base case    if (K == 0)        return max_sum;     // Pick the start index    int max_sum_start = max_sum + arr[start];     // Pick the end index    int max_sum_end = max_sum + arr[end];     // Recursive function call    int ans = max(        maxSum(arr, K - 1, start + 1, end, max_sum_start),        maxSum(arr, K - 1, start, end - 1, max_sum_end));     // Return the final answer    return ans;} // Function to find the maximized sumvoid maximizeSum(int arr[], int K, int n){    int max_sum = 0;    int start = 0;    int end = n - 1;    printf("%d",maxSum(arr, K, start, end, max_sum));} // Driver codeint main(){    int arr[] = { 8, 4, 4, 8, 12, 3, 2, 9 };    int K = 3;    int n = sizeof(arr) / sizeof(arr[0]);    maximizeSum(arr, K, n);    return 0;} // This code is contributed by Aditya Kumar (adityakumar129)
// Java program to maximize the sum of K elements// in the array by taking only corner elementsimport java.util.*; class GFG {     // Function to return maximum sum    static int maxSum(int arr[], int K, int start, int end, int max_sum)    {        // Base case        if (K == 0)            return max_sum;         // Pick the start index        int max_sum_start = max_sum + arr[start];         // Pick the end index        int max_sum_end = max_sum + arr[end];         // Recursive function call        int ans = Math.max(maxSum(arr, K - 1, start + 1, end, max_sum_start),                           maxSum(arr, K - 1, start, end - 1, max_sum_end));         // Return the final answer        return ans;    }     // Function to find the maximized sum    static void maximizeSum(int arr[], int K, int n)    {        int max_sum = 0;        int start = 0;        int end = n - 1;        System.out.print(maxSum(arr, K, start, end, max_sum));    }     // Driver code    public static void main(String[] args)    {        int arr[] = { 8, 4, 4, 8, 12, 3, 2, 9 };        int K = 3;        int n = arr.length;        maximizeSum(arr, K, n);    }} // This code is contributed by Aditya Kumar (adityakumar129)
# Python3 program to maximize the sum of K elements# in the array by taking only corner elements # Function to return maximum sumdef maxSum(arr, K, start, end, max_sum):         # Base case    if (K == 0):        return max_sum     # Pick the start index    max_sum_start = max_sum + arr[start]     # Pick the end index    max_sum_end = max_sum + arr[end]     # Recursive function call    ans = max(maxSum(arr,  K - 1, start + 1,                     end, max_sum_start),          maxSum(arr, K - 1, start,                     end - 1, max_sum_end))     # Return the final answer    return ans # Function to find the maximized sumdef maximizeSum(arr, K, n):    max_sum = 0    start = 0    end = n - 1     print(maxSum(arr, K, start, end, max_sum)) # Driver codeif __name__ == '__main__':         arr = [8, 4, 4, 8, 12, 3, 2, 9]    K = 3    n = len(arr)     maximizeSum(arr, K, n) # This code is contributed by Bhupendra_Singh
// C# program to maximize the sum of K elements// in the array by taking only corner elementsusing System;  class GFG{ // Function to return maximum sumstatic int maxSum(int []arr, int K,                  int start, int end,                  int max_sum){    // Base case    if (K == 0)        return max_sum;     // Pick the start index    int max_sum_start = max_sum + arr[start];     // Pick the end index    int max_sum_end = max_sum + arr[end];     // Recursive function call    int ans = Math.Max(maxSum(arr, K - 1, start + 1,                              end, max_sum_start),                       maxSum(arr, K - 1, start,                              end - 1, max_sum_end));     // Return the readonly answer    return ans;} // Function to find the maximized sumstatic void maximizeSum(int []arr, int K, int n){    int max_sum = 0;    int start = 0;    int end = n - 1;    Console.Write(maxSum(arr, K, start,                         end, max_sum));} // Driver codepublic static void Main(String[] args){    int []arr = { 8, 4, 4, 8, 12, 3, 2, 9 };    int K = 3;    int n = arr.Length;         maximizeSum(arr, K, n);}} // This code is contributed by sapnasingh4991
<script> // Javascript program to maximize the sum of K elements// in the array by taking only corner elements  // Function to return maximum sumfunction maxSum(arr, K,                  start, end,                  max_sum){    // Base case    if (K == 0)        return max_sum;      // Pick the start index    let max_sum_start = max_sum + arr[start];      // Pick the end index    let max_sum_end = max_sum + arr[end];      // Recursive function call    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 the final answer    return ans;}  // Function to find the maximized sumfunction maximizeSum(arr, K, n){    let max_sum = 0;    let start = 0;    let end = n - 1;    document.write(maxSum(arr, K, start,                            end, max_sum));}// Driver Code         let arr = [ 8, 4, 4, 8, 12, 3, 2, 9 ];    let K = 3;    let n = arr.length;    maximizeSum(arr, K, n);                     </script>
21
 
Time Complexity: O(2^N)
Auxiliary Space: O(N)
Efficient Approach: To solve the problem more efficiently we will implement Sliding Window concept. 
Initialize two integers with 0, curr_points and max_points to represents current points and maximum points respectively.
Now, iterate over K elements one by one from the beginning and form the window of size K, also update the value of curr_points by curr_points + arr[i] and max_points with the value of curr_points.
After that in each step, take one element from the end of the array and remove the rightmost element from the previously selected window with beginning elements where the window size always remains K. Update the values for curr_points and max_points accordingly. At last, we have K elements from the end of the array, and max_points contains the required result that has to be returned.
Let us look at the image below to understand it better:
Below is the implementation of the above approach: 
C++
C
Java
Python3
C#
Javascript
// C++ program to maximize the sum of K elements// in the array by taking only corner elements #include <bits/stdc++.h>using namespace std; // Function to return maximum sumint maxPointCount(int arr[], int K, int size){    // Initialize variables    int curr_points = 0;    int max_points = 0;     // Iterate over first K elements of array and update the    // value for curr_points    for (int i = 0; i < K; i++)        curr_points += arr[i];     // Update value for max_points    max_points = curr_points;     // j points to the end of the array    int j = size - 1;    for (int i = K - 1; i >= 0; i--) {        curr_points = curr_points + arr[j] - arr[i];        max_points = max(curr_points, max_points);        j--;    }    // Return the final result    return max_points;} // Driver codeint main(){    int arr[] = { 8, 4, 4, 8, 12, 3, 2, 9 };    int K = 3;    int n = sizeof(arr) / sizeof(arr[0]);    cout << maxPointCount(arr, K, n);    return 0;} // This code is contributed by Aditya Kumar (adityakumar129)
// C program to maximize the sum of K elements// in the array by taking only corner elements #include <stdio.h> // Find maximum between two numbers.int max(int num1, int num2){    return (num1 > num2 ) ? num1 : num2;} // Function to return maximum sumint maxPointCount(int arr[], int K, int size){    // Initialize variables    int curr_points = 0;    int max_points = 0;     // Iterate over first K elements of array and update the    // value for curr_points    for (int i = 0; i < K; i++)        curr_points += arr[i];     // Update value for max_points    max_points = curr_points;     // j points to the end of the array    int j = size - 1;    for (int i = K - 1; i >= 0; i--) {        curr_points = curr_points + arr[j] - arr[i];        max_points = max(curr_points, max_points);        j--;    }    // Return the final result    return max_points;} // Driver codeint main(){    int arr[] = { 8, 4, 4, 8, 12, 3, 2, 9 };    int K = 3;    int n = sizeof(arr) / sizeof(arr[0]);    printf("%d",maxPointCount(arr, K, n));    return 0;} // This code is contributed by Aditya Kumar (adityakumar129)
// Java program to maximize the sum of K elements in the// array by taking only corner elementsimport java.util.Arrays;import java.util.Scanner; class GFG {     // Function to return maximum sum    public static int maxPointCount(int arr[], int K, int size)    {        // Initialize variables        int curr_points = 0;        int max_points = 0;        // Iterate over first K elements of array and update        // the value for curr_points        for (int i = 0; i < K; i++)            curr_points += arr[i];        // Update value for max_points        max_points = curr_points;        // j points to the end of the array        int j = size - 1;        for (int i = K - 1; i >= 0; i--) {            curr_points = curr_points + arr[j] - arr[i];            max_points = Math.max(curr_points, max_points);            j--;        }        // Return the final result        return max_points;    }     // Driver code    public static void main(String args[])    {        int[] arr = { 8, 4, 4, 8, 12, 3, 2, 9 };        int K = 3;        int n = arr.length;        System.out.print(maxPointCount(arr, K, n));    }} // This code is contributed by Aditya Kumar (adityakumar129)
# Python3 program to maximize the sum# of K elements in the array by taking# only corner elements # Function to return maximum sumdef maxPointCount(arr, K, size):     # Initialize variables    curr_points = 0    max_points = 0     # Iterate over first K elements    # of array and update the value    # for curr_points    for i in range(K):        curr_points += arr[i]     # Update value for max_points    max_points = curr_points     # j points to the end of the array    j = size - 1     for i in range(K - 1, -1, -1):        curr_points = (curr_points +                       arr[j] - arr[i])        max_points = max(curr_points,                         max_points)        j -= 1     # Return the final result    return max_points # Driver codeif __name__ == "__main__":         arr = [ 8, 4, 4, 8, 12, 3, 2, 9 ]    K = 3    n = len(arr)     print(maxPointCount(arr, K, n)) # This code is contributed by chitranayal
// C# program to maximize the sum// of K elements in the array by// taking only corner elementsusing System;class GFG{ // Function to return maximum sumpublic static int maxPointCount(int []arr,                                int K,                                int size){         // Initialize variables    int curr_points = 0;    int max_points = 0;     // Iterate over first K elements    // of array and update the value    // for curr_points    for(int i = 0; i < K; i++)        curr_points += arr[i];     // Update value for max_points    max_points = curr_points;     // j points to the end of the array    int j = size - 1;     for(int i = K - 1; i >= 0; i--)    {        curr_points = curr_points +                      arr[j] - arr[i];        max_points = Math.Max(curr_points,                              max_points);        j--;    }     // Return the readonly result    return max_points;} // Driver codepublic static void Main(String []args){    int []arr = { 8, 4, 4, 8, 12, 3, 2, 9 };    int K = 3;    int n = arr.Length;     Console.Write( maxPointCount(arr, K, n));}} // This code is contributed by sapnasingh4991
<script> // JavaScript program to maximize the sum// of K elements in the array by// taking only corner elements // Function to return maximum sumfunction maxPointCount(arr,k,size){    // Initialize variables    let curr_points = 0;    let max_points = 0;      // Iterate over first K elements    // of array and update the value    // for curr_points    for(let i = 0; i < K; i++)        curr_points += arr[i];      // Update value for max_points    max_points = curr_points;      // j points to the end of the array    let j = size - 1;      for(let i = K - 1; i >= 0; i--)    {        curr_points = curr_points +                      arr[j] - arr[i];        max_points = Math.max(curr_points,                              max_points);        j--;    }      // Return the final result    return max_points;} // Driver codelet arr=[8, 4, 4, 8, 12, 3, 2, 9];let K = 3;let n = arr.length;document.write( maxPointCount(arr, K, n));  // This code is contributed by avanitrachhadiya2155 </script>
21
Time Complexity: O(N), where N is size of the array.Auxiliary Space Complexity: O(1).
bgangwar59
GauravRajput1
sapnasingh4991
SoumikMondal
ukasp
nabanita bág
TanyaSethi
souravghosh0416
avanitrachhadiya2155
saurabh1990aror
subham348
adityakumar129
akhilchouhan
sliding-window
Algorithms
Arrays
Mathematical
Recursion
sliding-window
Arrays
Mathematical
Recursion
Algorithms
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.