Maximum removals possible from an array such that sum of its elements is greater than or equal to that of another array
Last Updated :
04 May, 2021
Given two arrays arr[] and brr[] of size N and M respectively, the task is to count the maximum number of elements that can be removed from the array arr[] such that the sum of elements in arr[] is greater than or equal to the sum of elements in brr[].
Examples:
Input: arr[] = { 1, 2, 4, 6 }, brr[] = { 7 }
Output: 2
Explanation:
Removing arr[2] modifies arr[] to { 1, 2, 6 } and sum equal to 9 (> 7)
Removing arr[1] modifies arr[] to { 1, 6 } and sum equal to 7 (&gr; 7)
Removing arr[0] modifies arr[] to { 6 } and sum equal to 6 (< 7)
Therefore, the required output is 2.
Input: arr[] = { 10, 20 }, brr[] = { 5 }
Output: 1
Explanation:
Removing arr[1] modifies arr[] to { 10 } and sum equal to 10 (> 5)
Removing arr[0] modifies arr[] to { } and sum equal to 0 (< 5)
Therefore, the required output is 1.
Approach: The problem can be solved using Greedy technique. Follow the steps below to solve the problem:
- Sort the array, arr[] in ascending order.
- Remove the smallest element from the array arr[] and check if the sum of elements of arr[] is greater than or equal to sum of array elements brr[] or not. If found to be true, then increment the count.
- Finally, print the count obtained.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxCntRemovedfromArray( int arr[], int N,
int brr[], int M)
{
sort(arr, arr + N);
int i = 0;
int sumArr = 0;
for ( int i = 0; i < N; i++) {
sumArr += arr[i];
}
int sumBrr = 0;
for ( int i = 0; i < M; i++) {
sumBrr += brr[i];
}
int cntRemElem = 0;
while (i < N and sumArr >= sumBrr) {
sumArr -= arr[i];
i += 1;
if (sumArr >= sumBrr) {
cntRemElem += 1;
}
}
return cntRemElem;
}
int main()
{
int arr[] = { 1, 2, 4, 6 };
int brr[] = { 7 };
int N = sizeof (arr) / sizeof (arr[0]);
int M = sizeof (brr) / sizeof (brr[0]);
cout << maxCntRemovedfromArray(arr, N, brr, M);
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
static int maxCntRemovedfromArray( int [] arr, int N,
int [] brr, int M)
{
Arrays.sort(arr);
int i = 0 ;
int sumArr = 0 ;
for (i = 0 ; i < N; i++)
{
sumArr += arr[i];
}
int sumBrr = 0 ;
for (i = 0 ; i < M; i++)
{
sumBrr += brr[i];
}
int cntRemElem = 0 ;
while (i < N && sumArr >= sumBrr) {
sumArr -= arr[i];
i += 1 ;
if (sumArr >= sumBrr) {
cntRemElem += 1 ;
}
}
return cntRemElem;
}
public static void main(String[] args)
{
int [] arr = new int [] { 1 , 2 , 4 , 6 };
int [] brr = new int [] { 7 };
int N = arr.length;
int M = brr.length;
System.out.println(
maxCntRemovedfromArray(arr, N, brr, M));
}
}
|
Python3
def maxCntRemovedfromArray(arr, N, brr, M):
arr.sort(reverse = False )
i = 0
sumArr = 0
for i in range (N):
sumArr + = arr[i]
sumBrr = 0
for i in range (M):
sumBrr + = brr[i]
cntRemElem = 0
while (i < N and sumArr > = sumBrr):
sumArr - = arr[i]
i + = 1
if (sumArr > = sumBrr):
cntRemElem + = 1
return cntRemElem
if __name__ = = '__main__' :
arr = [ 1 , 2 , 4 , 6 ]
brr = [ 7 ]
N = len (arr)
M = len (brr)
print (maxCntRemovedfromArray(arr, N, brr, M))
|
C#
using System;
class GFG
{
static int maxCntRemovedfromArray( int [] arr, int N,
int [] brr, int M)
{
Array.Sort(arr);
int i = 0;
int sumArr = 0;
for (i = 0; i < N; i++)
{
sumArr += arr[i];
}
int sumBrr = 0;
for (i = 0; i < M; i++)
{
sumBrr += brr[i];
}
int cntRemElem = 0;
while (i < N && sumArr >= sumBrr)
{
sumArr -= arr[i];
i += 1;
if (sumArr >= sumBrr)
{
cntRemElem += 1;
}
}
return cntRemElem;
}
static public void Main()
{
int [] arr = new int [] { 1, 2, 4, 6 };
int [] brr = new int [] { 7 };
int N = arr.Length;
int M = brr.Length;
Console.WriteLine(
maxCntRemovedfromArray(arr, N, brr, M));
}
}
|
Javascript
<script>
function maxCntRemovedfromArray(arr, N, brr, M)
{
arr.sort();
let i = 0;
let sumArr = 0;
for (i = 0; i < N; i++)
{
sumArr += arr[i];
}
let sumBrr = 0;
for (i = 0; i < M; i++)
{
sumBrr += brr[i];
}
let cntRemElem = 0;
while (i < N && sumArr >= sumBrr) {
sumArr -= arr[i];
i += 1;
if (sumArr >= sumBrr) {
cntRemElem += 1;
}
}
return cntRemElem;
}
let arr = [ 1, 2, 4, 6 ];
let brr = [ 7 ];
let N = arr.length;
let M = brr.length;
document.write(
maxCntRemovedfromArray(arr, N, brr, M)
);
</script>
|
Time Complexity: O(N * log(N))
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...