Maximize deletions by removing prefix and suffix of Array with same sum
Last Updated :
16 Sep, 2022
Given an array Arr[] of size N, the cost of removing ith element is Arr[i]. The task is to remove the maximum number of elements by removing the prefix and the suffix of the same length and having the same total cost.
Examples:
Input: Arr[] = {80, 90, 81, 80}
Output: 2
Explanation: If we choose 80 from front ( left side cost = 80),
and choose 80 from back (right side cost = 80), both are same.
But when we choose 90 from front or 81 from back
the costs would not remain same.
So maximum 2 elements can be removed from the array.
Input: Arr[] = { 8, 5, 7, 8, 7, 6, 7}
Output: 6
Explanation: It will be optimal to select 8, 5, 7 from the front
( left side cost = 20), and 7, 6, 7 from the back (right side cost = 20),
which results in a maximum of 6 elements ( {8, 5, 7}, {7, 6, 7} )
that can be removed from the array.
Approach: To solve the problem use the following idea:
Since we have to equalize the cost of removing elements, we should know the sum of costs from both ends. Sum needs to be calculated from front and back for each element, so prefix sum and suffix sum can be used to store the sums from both ends.
Then traverse the suffix sum array and find the lower bound if the same in the prefix sum array. The maximum number of elements found is the required answer.
Follow the below illustration for a better understanding.
Illustration:
Consider the array Arr[] = {8, 5, 7, 8, 7, 6, 7}
Prefix sum array = {8, 13, 20, 28, 35, 41, 48}
Suffix sum array = {48, 40, 35, 28, 20, 13, 7}
For 7 in suffix array:
=> The lower bound is 8 in the prefix array.
=> No elements can be deleted.
For 13 in suffix array:
=> The lower bound is 13 in the prefix array.
=> Elements deleted = 2 + 2 = 4.
For 20 in suffix array:
=> The lower bound is 20 in the prefix array.
=> Elements deleted = 3+3 = 6.
For 28 in suffix array:
=> The lower bound is 28 in the prefix array.
=> The index for both of them is same, that is the same element is considered twice.
Hence maximum 6 elements can be deleted.
Follow the given steps to solve the problem:
- Initialize prefix sum and suffix sum array of size N, and assign their each element equal to the given array element.
- Calculate the prefix sum and suffix sum and store them in their respective arrays.
- iterate over the suffix sum array from end:
- Perform lower bound on prefix sum array for that sum.
- Get the index of the lower bound.
- If the lower bound value and suffix sum are the same, calculate the total number of elements deleted.
- Update the answer to store the maximum number of elements.
Below is the implementation for the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void solve( int v[], int n)
{
vector< int > pref(n);
vector< int > suff(n);
for ( int i = 0; i < n; i++) {
pref[i] = suff[i] = v[i];
}
for ( int i = 1; i < n; i++) {
pref[i] = pref[i] + pref[i - 1];
}
for ( int i = n - 2; i >= 0; i--) {
suff[i] = suff[i] + suff[i + 1];
}
int ans = 0;
for ( int i = n - 1; i >= 1; i--) {
auto z = lower_bound(pref.begin(),
pref.begin() + i - 1, suff[i]);
int idx = z - pref.begin();
if (pref[idx] == suff[i]) {
int temp = 0;
temp = temp + (n - i);
temp = temp + (idx + 1);
ans = max(ans, temp);
}
}
cout << ans;
}
int main()
{
int arr[] = { 70, 80, 85, 70, 80 };
int N = sizeof (arr) / sizeof (arr[0]);
solve(arr, N);
return 0;
}
|
Java
import java.io.*;
class GFG {
public static int lower_bound( int arr[], int l, int r,
int key)
{
int low = l;
int high = r - 1 ;
while (low < high) {
int mid = low + (high - low) / 2 ;
if (arr[mid] >= key) {
high = mid;
}
else {
low = mid + 1 ;
}
}
return low;
}
public static void solve( int v[], int n)
{
int pref[] = new int [n];
int suff[] = new int [n];
for ( int i = 0 ; i < n; i++) {
pref[i] = suff[i] = v[i];
}
for ( int i = 1 ; i < n; i++) {
pref[i] = pref[i] + pref[i - 1 ];
}
for ( int i = n - 2 ; i >= 0 ; i--) {
suff[i] = suff[i] + suff[i + 1 ];
}
int ans = 0 ;
for ( int i = n - 1 ; i >= 1 ; i--) {
int idx = lower_bound(pref, 0 , i - 1 , suff[i]);
if (pref[idx] == suff[i]) {
int temp = 0 ;
temp = temp + (n - i);
temp = temp + (idx + 1 );
ans = Math.max(ans, temp);
}
}
System.out.print(ans);
}
public static void main(String[] args)
{
int arr[] = { 70 , 80 , 85 , 70 , 80 };
int N = arr.length;
solve(arr, N);
}
}
|
Python3
from bisect import bisect_left
def solve(v, n):
pref = [ 0 for _ in range (n)]
suff = [ 0 for _ in range (n)]
for i in range ( 0 , n):
pref[i] = suff[i] = v[i]
for i in range ( 1 , n):
pref[i] = pref[i] + pref[i - 1 ]
for i in range (n - 2 , - 1 , - 1 ):
suff[i] = suff[i] + suff[i + 1 ]
ans = 0
for i in range (n - 1 , 0 , - 1 ):
z = bisect_left(pref, suff[i], lo = 0 , hi = i - 1 )
idx = z
if (pref[idx] = = suff[i]):
temp = 0
temp = temp + (n - i)
temp = temp + (idx + 1 )
ans = max (ans, temp)
print (ans)
if __name__ = = "__main__" :
arr = [ 70 , 80 , 85 , 70 , 80 ]
N = len (arr)
solve(arr, N)
|
C#
using System;
using System.Collections.Generic;
public class GFG{
public static int lower_bound( int [] arr, int l, int r,
int key)
{
int low = l;
int high = r - 1;
while (low < high) {
int mid = low + (high - low) / 2;
if (arr[mid] >= key) {
high = mid;
}
else {
low = mid + 1;
}
}
return low;
}
public static void solve( int [] v, int n)
{
int [] pref = new int [n];
int [] suff = new int [n];
for ( int i = 0; i < n; i++) {
pref[i] = suff[i] = v[i];
}
for ( int i = 1; i < n; i++) {
pref[i] = pref[i] + pref[i - 1];
}
for ( int i = n - 2; i >= 0; i--) {
suff[i] = suff[i] + suff[i + 1];
}
int ans = 0;
for ( int i = n - 1; i >= 1; i--) {
int idx = lower_bound(pref, 0, i - 1, suff[i]);
if (pref[idx] == suff[i]) {
int temp = 0;
temp = temp + (n - i);
temp = temp + (idx + 1);
ans = Math.Max(ans, temp);
}
}
Console.Write(ans);
}
static public void Main ()
{
int [] arr = { 70, 80, 85, 70, 80 };
int N = arr.Length;
solve(arr, N);
}
}
|
Javascript
let lowerBound = (A,N, T) => {
let i = 0,
j = N;
while (i < j) {
let k = Math.floor((i + j) / 2);
if (A[k] < T)
i = k + 1;
else
j = k;
}
return i;
};
function solve( v, n)
{
let pref = new Array(n);
let suff = new Array(n);
for (let i = 0; i < n; i++) {
pref[i] = suff[i] = v[i];
}
for (let i = 1; i < n; i++) {
pref[i] = pref[i] + pref[i - 1];
}
for (let i = n - 2; i >= 0; i--) {
suff[i] = suff[i] + suff[i + 1];
}
let ans = 0;
for (let i = n - 1; i >= 1; i--) {
let z = lowerBound(pref,
i - 1, suff[i]);
let idx = z;
if (pref[idx] == suff[i]) {
let temp = 0;
temp = temp + (n - i);
temp = temp + (idx + 1);
ans = Math.max(ans, temp);
}
}
console.log(ans);
}
let arr = [ 70, 80, 85, 70, 80 ];
let N = arr.length;
solve(arr, N);
|
Time Complexity: O(N * logN)
Space Complexity: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...