Remove k corner elements to maximize remaining sum
Last Updated :
31 Aug, 2021
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 the left corner, then we need to remove 3 elements from the 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 : 18
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 the result, update the result.
Time Complexity: O(n * k)
Efficient Approach (Using Window Sliding Technique)
1) Find the sum of first n-k elements and initialize this as a current sum and also initialize this as result.
2) 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 2, 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 the above problem statement.
C++
#include <bits/stdc++.h>
using namespace std;
int calculate( int arr[], int n, int k)
{
int curr_sum = 0;
for ( int i = 0; i < n - k; i++)
curr_sum += arr[i];
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);
}
return res;
}
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;
}
|
Java
import java.util.*;
class GFG{
static int calculate( int [] arr,
int n, int k)
{
int total_sum = 0 ;
for ( int i = 0 ; i < n; i++)
total_sum += arr[i];
int curr_sum = 0 ;
for ( int i = 0 ; i < n - k; i++)
curr_sum += arr[i];
int res = curr_sum;
for ( int i = n - k; i < n; i++)
{
curr_sum = curr_sum -
arr[i - n + k] +
arr[i];
res = Math.max(res, curr_sum);
}
return res;
}
public static void main(String[] args)
{
int [] arr = { 11 , 49 , 100 ,
20 , 86 , 29 , 72 };
int n = arr.length;
int k = 4 ;
System.out.print( "Maximum sum of remaining " +
"elements " +
calculate(arr, n, k) + "\n" );
}
}
|
Python3
def calculate(arr, n, k):
total_sum = 0
for i in arr:
total_sum + = i
curr_sum = 0
for i in range (n - k):
curr_sum + = arr[i]
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)
return res
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))
|
C#
using System;
using System.Collections;
using System.Collections.Generic;
class GFG{
static int calculate( int []arr, int n, int k)
{
int total_sum = 0;
for ( int i = 0; i < n; i++)
total_sum += arr[i];
int curr_sum = 0;
for ( int i = 0; i < n - k; i++)
curr_sum += arr[i];
int res = curr_sum;
for ( int i = n - k; i < n; i++)
{
curr_sum = curr_sum -
arr[i - n + k] + arr[i];
res = Math.Max(res, curr_sum);
}
return res;
}
public static void Main( string [] args)
{
int []arr = { 11, 49, 100, 20, 86, 29, 72 };
int n = arr.Length;
int k = 4;
Console.Write( "Maximum sum of remaining " +
"elements " +
calculate(arr, n, k) + "\n" );
}
}
|
Javascript
<script>
function calculate(arr, n, k)
{
let total_sum = 0;
for (let i = 0; i < n; i++)
total_sum += arr[i];
let curr_sum = 0;
for (let i = 0; i < n - k; i++)
curr_sum += arr[i];
let res = curr_sum;
for (let i = n - k; i < n; i++) {
curr_sum = curr_sum - arr[i - n + k] + arr[i];
res = Math.max(res, curr_sum);
}
return res;
}
let arr = [11, 49, 100, 20, 86, 29, 72];
let n = arr.length;
let k = 4;
document.write( "Maximum sum of remaining elements " + calculate(arr, n, k) + "<br>" );
</script>
|
Output:
Maximum sum of remaining elements 206
Time Complexity: O(k)
Auxiliary Space : O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...