# Minimum size of subset of pairs whose sum is at least the remaining array elements

• Last Updated : 24 Feb, 2022

Given two arrays A[] and B[] both consisting of N positive integers, the task is to find the minimum size of the subsets of pair of elements (A[i], B[i]) such that the sum of all the pairs of subsets is at least the sum of remaining array elements A[] which are not including in the subset i.e., (A[0] + B[0] + A[1] + B[1] + … + A[K – 1] + B[K – 1] >= A[K + 1] + … + A[N – 1].

Examples:

Input: A[] = {3, 2, 2}, B[] = {2, 3, 1}
Output: 1
Explanation:
Choose the subset as {(3, 2)}. Now, the sum of subsets is 3 + 2 = 5, which greater than sum of remaining A[] = 3 + 1 = 4.

Input: A[] = {2, 2, 2, 2, 2}, B[] = {1, 1, 1, 1, 1}
Output: 3

Naive Approach: The simplest approach is to generate all possible subsets of the given pairs of elements and print the size of that subset that satisfies the given criteria and has a minimum size.

Time Complexity: O(2N)
Auxiliary Space: O(1)

Efficient Approach: The given problem can be solved by using the Greedy Approach, the idea is to use sorting and observation can be made that choosing the ith pair as a part of the resultant subset the difference between the 2 subsets decreases by the amount (2*S[i] + U[i]). Follow the steps below to solve the problem:

• Initialize an array, say difference[] of size N that stores the value of (2*A[i] + B[i]) for each pair of elements.
• Initialize a variable, say sum that keeps the track of the sum of remaining array elements A[i].
• Initialize a variable, say K that keeps the track of the size of the resultant subset.
• Iterate over the range [0, N) and update the value of difference[i] as the value of (2*A[i] + B[i]) and decrement the value of sum by the value A[i].
• Sort the given array difference[] in increasing order.
• Traverse the array difference[] in a reverse manner until the sum is negative and add the value of difference[i] to the sum and increment the value of K by 1.
• After completing the above steps, print the value of K as the result.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `// Function to find the minimum size``// of subset satisfying given criteria``void` `maximizeApples(vector<``int``>& U, vector<``int``>& S, ``int` `N)``{``  ` `    ``// Stores the value of 2*S[i] + U[i]``    ``vector<``int``> x(N);` `    ``// Stores the difference between``    ``// the 2 subsets``    ``int` `sum = 0;` `    ``for` `(``int` `i = 0; i < N; i++) {` `        ``// Update the value of sum``        ``sum -= S[i];``        ``x[i] += 2 * S[i] + U[i];``    ``}` `    ``// Sort the array X[] in an``    ``// ascending order``    ``sort(x.begin(), x.end());``    ``int` `ans = 0;``    ``int` `j = N - 1;` `    ``// Traverse the array``    ``while` `(sum <= 0) {` `        ``// Update the value of sum``        ``sum += x[j--];` `        ``// Increment value of ans``        ``ans++;``    ``}` `    ``// Print the resultant ans``    ``cout << ans;``}` `// Driver Code``int` `main()``{``    ``vector<``int``> A = { 1, 1, 1, 1, 1 };``    ``vector<``int``> B = { 2, 2, 2, 2, 2 };``    ``int` `N = A.size();``    ``maximizeApples(A, B, N);` `    ``return` `0;``}` `    ``// This code is contributed by rakeshsahni`

## Java

 `// Java program for the above approach` `import` `java.io.*;``import` `java.util.*;` `class` `GFG {` `    ``// Function to find the minimum size``    ``// of subset satisfying given criteria``    ``private` `static` `void` `maximizeApples(``        ``int``[] U, ``int``[] S, ``int` `N)``    ``{``        ``// Stores the value of 2*S[i] + U[i]``        ``int``[] x = ``new` `int``[N];` `        ``// Stores the difference between``        ``// the 2 subsets``        ``int` `sum = ``0``;` `        ``for` `(``int` `i = ``0``; i < N; i++) {` `            ``// Update the value of sum``            ``sum -= S[i];``            ``x[i] += ``2` `* S[i] + U[i];``        ``}` `        ``// Sort the array X[] in an``        ``// ascending order``        ``Arrays.sort(x);``        ``int` `ans = ``0``;``        ``int` `j = N - ``1``;` `        ``// Traverse the array``        ``while` `(sum <= ``0``) {` `            ``// Update the value of sum``            ``sum += x[j--];` `            ``// Increment value of ans``            ``ans++;``        ``}` `        ``// Print the resultant ans``        ``System.out.println(ans);``    ``}` `    ``// Driver Code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``int``[] A = ``new` `int``[] { ``1``, ``1``, ``1``, ``1``, ``1` `};``        ``int``[] B = ``new` `int``[] { ``2``, ``2``, ``2``, ``2``, ``2` `};``        ``int` `N = A.length;``        ``maximizeApples(A, B, N);``    ``}``}`

## Python3

 `# Python program for the above approach` `# Function to find the minimum size``# of subset satisfying given criteria``def` `maximizeApples(U, S, N):` `    ``# Stores the value of 2*S[i] + U[i]``    ``x ``=` `[``0``]``*``N` `    ``# Stores the difference between``    ``# the 2 subsets``    ``sum` `=` `0` `    ``for` `i ``in` `range``(N):` `        ``# Update the value of sum``        ``sum` `-``=` `S[i]``        ``x[i] ``+``=` `2` `*` `S[i] ``+` `U[i]` `    ``# Sort the array X[] in an``    ``# ascending order``    ``x.sort()``    ``ans ``=` `0``    ``j ``=` `N ``-` `1` `    ``# Traverse the array``    ``while` `sum` `<``=` `0``:` `        ``# Update the value of sum``        ``sum` `+``=` `x[j]``        ``j ``=` `j ``-` `1` `        ``# Increment value of ans``        ``ans ``=` `ans ``+` `1` `    ``# Print the resultant ans``    ``print``(ans)` `# Driver Code``A ``=` `[``1``, ``1``, ``1``, ``1``, ``1``]``B ``=` `[``2``, ``2``, ``2``, ``2``, ``2``]``N ``=` `len``(A)``maximizeApples(A, B, N)` `# This code is contributed by Potta Lokesh`

## C#

 `//  c# program for the above approach``using` `System.Collections.Generic;``using` `System;``class` `GFG``{` `    ``// Function to find the minimum size``    ``// of subset satisfying given criteria``    ``static` `void` `maximizeApples(``int``[] U, ``int``[] S, ``int` `N)``    ``{``      ` `        ``// Stores the value of 2*S[i] + U[i]``        ``List<``int``> x = ``new` `List<``int``>();``      ` `        ``// Stores the difference between``        ``// the 2 subsets``        ``int` `sum = 0;` `        ``for` `(``int` `i = 0; i < N; i++)``        ``{``          ` `            ``// Update the value of sum``            ``sum -= S[i];``            ``x.Add( 2 * S[i] + U[i]);``        ``}` `        ``// Sort the array X[] in an``        ``// ascending order``        ``x.Sort();``        ``int` `ans = 0;``        ``int` `j = N - 1;` `        ``// Traverse the array``        ``while` `(sum <= 0) {` `            ``// Update the value of sum``            ``sum += x[j--];` `            ``// Increment value of ans``            ``ans++;``        ``}` `        ``// Print the resultant ans``        ``Console.WriteLine(ans);``    ``}` `    ``// Driver Code``    ``public` `static` `void` `Main()``    ``{``        ``int``[] A = ``new` `int``[] { 1, 1, 1, 1, 1 };``        ``int``[] B = ``new` `int``[] { 2, 2, 2, 2, 2 };``        ``int` `N = A.Length;``        ``maximizeApples(A, B, N);``    ``}``}` `// This code is contributed by amreshkumar3.`

## Javascript

 ``

Output:

`3`

Time Complexity: O(N*log N)
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up