Skip to content
Related Articles

Related Articles

Improve Article

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

  • Difficulty Level : Basic
  • Last Updated : 10 May, 2021
Geek Week

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:
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} 
Output: 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. 
 

  1. First to calculate the total sum of the array.
  2. 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.
  3. Then, add remaining sum to the result.
  4. 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;
}

Java




// Java program to maximize the sum
// of sum of the Array by removing
// end elements
import java.util.*;
 
class GFG{
     
// Function to find
// the maximum sum of sum
static 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
public static void main(String args[])
{
    int arr[] = { 3, 1, 7, 2, 1 };
    int N = arr.length;
 
    System.out.println(maxRemainingSum(arr, N));
}
}
 
// This code is contributed by ankitkumar34

Python3




# Python3 program to maximize the
# sum of sum of the Array by
# removing end elements
 
# Function to find the maximum
# sum of sum
def maxRemainingSum(arr, n):
 
    sum = 0
 
    # Compute the sum of whole array
    for i in range(n):
        sum += arr[i]
 
    i = 0
    j = n - 1
 
    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 += 1
 
        # If the right end element
        # is smaller than left end
        else:
 
            # Remove the right end element
            sum -= arr[j]
            j -= 1
     
        # Add the remaining element
        # sum in the result
        result += sum;
 
    # Return the maximum
    # sum of sum
    return result
 
# Driver code
arr = [ 3, 1, 7, 2, 1 ]
N = len(arr)
 
print(maxRemainingSum(arr, N))
 
# This code is contributed by ankitkumar34

C#




// C# program to maximize the sum
// of sum of the Array by removing
// end elements
using System;
 
class GFG{
     
// Function to find
// the maximum sum of sum
static int maxRemainingSum(int[] arr, int n)
{
    int i, sum = 0;
 
    // Compute the sum of whole array
    for(i = 0; i < n; i++)
        sum += arr[i];
 
    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
public static void Main()
{
    int[] arr = { 3, 1, 7, 2, 1 };
    int N = arr.Length;
 
    Console.Write(maxRemainingSum(arr, N));
}
}
 
// This code is contributed by chitranayal

Javascript




<script>
 
// Javascript program to maximize the sum
// of sum of the Array by
// removing end elements
 
// Function to find
// the maximum sum of sum
function maxRemainingSum(arr, n)
{
    var sum = 0;
 
    // compute the sum of whole array
    for (var i = 0; i < n; i++)
        sum += arr[i];
 
    var i = 0;
    var j = n - 1;
 
    var 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
var arr = [ 3, 1, 7, 2, 1 ];
var N = arr.length;
document.write( maxRemainingSum(arr, N));
 
</script>
Output: 
39

 

Time complexity: O(N)
 

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :