Maximize the sum of sum of the Array by removing end elements
Last Updated :
27 Dec, 2023
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}
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.
- 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++
#include <iostream>
using namespace std;
int maxRemainingSum( int arr[], int n)
{
int sum = 0;
for ( int i = 0; i < n; i++)
sum += arr[i];
int i = 0;
int j = n - 1;
int result = 0;
while (i < j)
{
if (arr[i] < arr[j])
{
sum -= arr[i];
i++;
}
else
{
sum -= arr[j];
j--;
}
result += sum;
}
return result;
}
int main()
{
int arr[] = {3, 1, 7, 2, 1 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << maxRemainingSum(arr, N);
return 0;
}
|
Java
import java.util.Arrays;
public class MaxRemainingSum {
static int maxRemainingSum( int [] arr, int n) {
int sum = Arrays.stream(arr).sum();
int i = 0 ;
int j = n - 1 ;
int result = 0 ;
while (i < j) {
if (arr[i] < arr[j]) {
sum -= arr[i];
i++;
} else {
sum -= arr[j];
j--;
}
result += sum;
}
return result;
}
public static void main(String[] args) {
int [] arr = { 3 , 1 , 7 , 2 , 1 };
int N = arr.length;
System.out.println(maxRemainingSum(arr, N));
}
}
|
Python3
def max_remaining_sum(arr, n):
total_sum = sum (arr)
i = 0
j = n - 1
result = 0
while i < j:
if arr[i] < arr[j]:
total_sum - = arr[i]
i + = 1
else :
total_sum - = arr[j]
j - = 1
result + = total_sum
return result
arr = [ 3 , 1 , 7 , 2 , 1 ]
N = len (arr)
print (max_remaining_sum(arr, N))
|
C#
using System;
class Program
{
static int MaxRemainingSum( int [] arr, int n)
{
int sum = 0;
for ( int i = 0; i < n; i++)
sum += arr[i];
int left = 0;
int right = n - 1;
int result = 0;
while (left < right)
{
if (arr[left] < arr[right])
{
sum -= arr[left];
left++;
}
else
{
sum -= arr[right];
right--;
}
result += sum;
}
return result;
}
static void Main()
{
int [] arr = {3, 1, 7, 2, 1 };
int N = arr.Length;
Console.WriteLine(MaxRemainingSum(arr, N));
}
}
|
Javascript
function maxRemainingSum(arr) {
let sum = 0;
for (let i = 0; i < arr.length; i++)
sum += arr[i];
let left = 0;
let right = arr.length - 1;
let result = 0;
while (left < right) {
if (arr[left] < arr[right]) {
sum -= arr[left];
left++;
} else {
sum -= arr[right];
right--;
}
result += sum;
}
return result;
}
const arr = [3, 1, 7, 2, 1 ];
console.log(maxRemainingSum(arr));
|
Time complexity: O(N), where N is the size of the given array.
Auxiliary space: O(1), as constant space is used.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...