Given an array, the task is to remove total k elements from corners to maximize the sum of remaining elements. For example, if we k = 5 and if we remove 2 elements from left corner, then we need to remove 3 elements from right corner.

**Examples:**

Input: arr = [11, 49, 100, 20, 86, 29, 72], k = 4

Output: 206

Explanation :: We remove 29 and 72 from right corner. We also remove 11 and 49 from left corner to get the maximum sum as 206 for remaining elements.

Input: arr[] = [1, 2, 3, 4, 5, 6, 1], k = 3

Output : 12

Explanation :: We remove two elements from left corner (1 and 2) and one element from right corner (1).

**Naive Approach : **

1) Initialize result as negative infinity.

2) Compute total sum.

3) Run a loop for x = 1 to k

…..Remove ‘x’ elements from left side and k – i elements from right side.

…..If the remaining elements have sum more than result, update the result.

Time Complexity : O(n * k)

Efficient Approach (Using Window Sliding Technique)

1) Find the total sum of array.

2) Find sum of first n-k elements and initialize this as current sum and also initialize this as result.

3) Run a loop for i = n-k to n-1

….curr_sum = curr_sum – arr[i – n + k] + arr[i]

….res = max(res, curr_sum)

In step 3, we mainly run sliding window. We remove an element from left side and add an element from right side.

Below is the c++ implementation of above problem statement.

## CPP

`#include <bits/stdc++.h> ` `using` `namespace` `std; ` `int` `calculate(` `int` `arr[], ` `int` `n, ` `int` `k) ` `{ ` ` ` `// calculate the total sum of all elements ` ` ` `// present in the array.. ` ` ` `int` `total_sum = 0; ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `total_sum += arr[i]; ` ` ` ` ` `// now calculate the sum of all elements ` ` ` `// excluding the last k elements.. ` ` ` `int` `curr_sum = 0; ` ` ` `for` `(` `int` `i = 0; i < n - k; i++) ` ` ` `curr_sum += arr[i]; ` ` ` ` ` `// now here its time to use sliding window ` ` ` `// concept, remove the first element from ` ` ` `// the current window and add the new element ` ` ` `// in it in order to get the sum of all n-k size ` ` ` `// of elements in arr. ` ` ` `// Calculate the minimum sum of elements of ` ` ` `// size n-k and stored it into the result ` ` ` `int` `res = curr_sum; ` ` ` `for` `(` `int` `i = n - k; i < n; i++) { ` ` ` `curr_sum = curr_sum - arr[i - n + k] + arr[i]; ` ` ` `res = max(res, curr_sum); ` ` ` `} ` ` ` ` ` `// Now return result (sum of remaining n-k elements) ` ` ` `return` `res; ` `} ` ` ` `// main function ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 11, 49, 100, 20, 86, 29, 72 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` `int` `k = 4; ` ` ` `cout << ` `"Maximum sum of remaining elements "` ` ` `<< calculate(arr, n, k) << ` `"\n"` `; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`def` `calculate(arr, n, k): ` ` ` ` ` `# calculate the total sum of all elements ` ` ` `# present in the array.. ` ` ` `total_sum ` `=` `0` ` ` `for` `i ` `in` `arr: ` ` ` `total_sum ` `+` `=` `i ` ` ` ` ` `# now calculate the sum of all elements ` ` ` `# excluding the last k elements.. ` ` ` `curr_sum ` `=` `0` ` ` `for` `i ` `in` `range` `(n ` `-` `k): ` ` ` `curr_sum ` `+` `=` `arr[i] ` ` ` ` ` `# now here its time to use sliding window ` ` ` `# concept, remove the first element from ` ` ` `# the current window and add the new element ` ` ` `# in it in order to get the sum of all n-k size ` ` ` `# of elements in arr. ` ` ` `# Calculate the minimum sum of elements of ` ` ` `# size n-k and stored it into the result ` ` ` `res ` `=` `curr_sum ` ` ` `for` `i ` `in` `range` `(n ` `-` `k, n): ` ` ` `curr_sum ` `=` `curr_sum ` `-` `arr[i ` `-` `n ` `+` `k] ` `+` `arr[i] ` ` ` `res ` `=` `max` `(res, curr_sum) ` ` ` ` ` `# Now return result (sum of remaining n-k elements) ` ` ` `return` `res ` ` ` `# main function ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `arr` `=` `[` `11` `, ` `49` `, ` `100` `, ` `20` `, ` `86` `, ` `29` `, ` `72` `] ` ` ` `n ` `=` `len` `(arr) ` ` ` `k ` `=` `4` ` ` `print` `(` `"Maximum sum of remaining elements "` `,calculate(arr, n, k)) ` ` ` `# This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

**Output:**

Maximum sum of remaining elements 206

** Time Complexity: ** O(1)

** Auxiliary Space :** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Maximize sum of atmost K elements in array by taking only corner elements | Set 2
- Maximize sum of K elements in Array by taking only corner elements
- Find last two remaining elements after removing median of any 3 consecutive elements repeatedly
- Average of remaining elements after removing K largest and K smallest elements from array
- Minimum number of elements to be removed such that the sum of the remaining elements is equal to k
- Remove an element to maximize the GCD of the given array
- Element equal to the sum of all the remaining elements
- Remove array end element to maximize the sum of product
- Check if the array has an element which is equal to XOR of remaining elements
- Count the subarray with sum strictly greater than the sum of remaining elements
- Check if the array has an element which is equal to sum of all the remaining elements
- Check if the array has an element which is equal to product of remaining elements
- Maximize the sum of differences of consecutive elements after removing exactly K elements
- Last element remaining by deleting two largest elements and replacing by their absolute difference if they are unequal
- Maximize the sum of X+Y elements by picking X and Y elements from 1st and 2nd array
- Maximize elements using another array
- Maximize number of elements from Array with sum at most K
- Maximize the sum of sum of the Array by removing end elements
- Maximize array elements upto given number
- Maximize count of corresponding same elements in given Arrays by Rotation

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.