# Maximize the sum of sum of the Array by removing end elements

Given an array **arr** of size **N**, the task is to maximize the sum of sum, of the remaining elements in the array, by removing the end elements.

**Example:**

Input:arr[] = {2, 3}

Output:3

Explanation:

At first we will delete 2, then sum of remaining elements = 3. Then delete 3. Therefore sum of sum = 3 + 0 = 3.

We can also delete 3 first and then 2, but in this case, the sum of sum = 2 + 0 = 2

But since 3 is larger, therefore the output is 3.

Input:arr[] = {3, 1, 7, 2, 1}

Ouput:39

Explanation:

At first we will delete 1 from last, then the sum of remaining elements

will be 13.

Then delete 2 from last, then the sum of remaining elements will be 11.

Then delete 3 from the beginning, then the sum of remaining elements will be 8.

Then we delete 1, the remaining sum is 7 and then delete 7.

Therefore the Sum of all remaining sums is 13 + 11 + 8 + 7 = 39, which is the maximum case.

**Approach:** The idea is to use Greedy Algorithm to solve this problem.

- First to calculate the total sum of the array.
- Then compare the elements on both ends and subtract the minimum value among the two, from the sum. This will make the remaining sum maximum.
- Then, add remaining sum to the result.
- Repeat the above steps till all the elements have been removed from the array. Then print the resultant sum.

Below is the implementation of the above approach:

## C++

`// C++ program to maximize the sum ` `// of sum of the Array by ` `// removing end elements ` ` ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `// Function to find ` `// the maximum sum of sum ` `int` `maxRemainingSum(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `int` `sum = 0; ` ` ` ` ` `// compute the sum of whole array ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `sum += arr[i]; ` ` ` ` ` `int` `i = 0; ` ` ` `int` `j = n - 1; ` ` ` ` ` `int` `result = 0; ` ` ` ` ` `// Traverse and remove the ` ` ` `// minimum value from an end ` ` ` `// to maximum the sum value ` ` ` `while` `(i < j) { ` ` ` ` ` `// If the left end element ` ` ` `// is smaller than right end ` ` ` `if` `(arr[i] < arr[j]) { ` ` ` ` ` `// remove the left end element ` ` ` `sum -= arr[i]; ` ` ` ` ` `i++; ` ` ` `} ` ` ` ` ` `// If the right end element ` ` ` `// is smaller than left end ` ` ` `else` `{ ` ` ` ` ` `// remove the right end element ` ` ` `sum -= arr[j]; ` ` ` `j--; ` ` ` `} ` ` ` ` ` `// Add the remaining element ` ` ` `// sum in the result ` ` ` `result += sum; ` ` ` `} ` ` ` ` ` `// Return the maximum ` ` ` `// sum of sum ` ` ` `return` `result; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 3, 1, 7, 2, 1 }; ` ` ` `int` `N = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `cout << maxRemainingSum(arr, N); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

39

**Time complexity: ** O(N)

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Maximize the sum of differences of consecutive elements after removing exactly K elements
- Average of remaining elements after removing K largest and K smallest elements from array
- 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
- Maximize the sum of X+Y elements by picking X and Y elements from 1st and 2nd array
- Maximize elements using another array
- Maximize the median of the given array after adding K elements to the same array
- Minimize the sum calculated by repeatedly removing any two elements and inserting their sum to the Array
- Maximize Array sum by swapping at most K elements with another array
- Maximize array elements upto given number
- Maximize array sum by concatenating corresponding elements of given two arrays
- Maximize count of distinct elements possible in an Array from the given operation
- Maximize the maximum subarray sum after removing atmost one element
- Maximize removal of adjacent array elements based on their absolute value
- Maximize occurrences of values between L and R on sequential addition of Array elements with modulo H
- Maximize distance between smallest and largest Array elements by a single swap
- Find last two remaining elements after removing median of any 3 consecutive elements repeatedly
- Minimize the sum of differences of consecutive elements after removing exactly K elements
- Maximum distinct elements after removing k elements
- Make two sets disjoint by removing minimum elements

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.