Smallest subset sum greater than all others
Given an array of non-negative integers, the task is to find the minimum number of elements such that their sum should be greater than the sum of the rest of the elements of the array.
Example:
Input: arr[] = [ 3 , 1 , 7, 1 ]
Output: 1
Explanation: Smallest subset is {7}. Sum of this subset is greater than the sum of all other elements left after removing subset {7} from the arrayInput: arr[] = [ 2 , 1 , 2 ]
Output: 2
Explanation: Smallest subset is {2 , 1}. Sum of this subset is greater than the sum of all other elements left after removing subset {2 , 1} from the array
Naive Approach - O(n x 2^n) Time
We generate all subsets and then find the subset having sum greater than the remaining elements. Generating all subsets takes O(2^n) time and finding the subset sum takes n time.
Expected Approach - O(n Log n) Time and O(1) Space
The idea to solve this problem is based on the greedy approach that we pick the largest elements to form the subset having sum greater than all others. We first sort the array in reverse order. Then we traverse the sorted array from the left (largest element) and compute sum of elements. If sum of current elements become more than the remaining sum, we return count of current elements as result.
#include <bits/stdc++.h>
using namespace std;
// function to find minimum elements needed
int minElements(vector<int>& arr) {
// calculate total sum of the array
int totalSum = accumulate(arr.begin(), arr.end(), 0);
// sort the array in descending order
sort(arr.begin(), arr.end(), greater<int>());
int res = 0, currSum = 0;
for (int x : arr) {
currSum += x;
res++;
// current sum greater than the remaining sum
if (currSum > totalSum - currSum)
return res;
}
return res;
}
int main() {
vector<int> arr = { 3, 1, 7, 1 };
cout << minElements(arr) << endl;
return 0;
}
// Java code to find minimum number of elements
// such that their sum is greater than sum of
// remaining elements of the array.
import java.io.*;
import java.util.*;
class GFG {
// Function to find minimum elements needed
static int minElements(int arr[], int n)
{
// Calculating HALF of array sum
int halfSum = 0;
for (int i = 0; i < n; i++)
halfSum = halfSum + arr[i];
halfSum = halfSum / 2;
// Sort the array in ascending order and
// start traversing array from the ascending
// sort in descending order.
Arrays.sort(arr);
int res = 0, curr_sum = 0;
for (int i = n - 1; i >= 0; i--) {
curr_sum += arr[i];
res++;
// Current sum greater than sum
if (curr_sum > halfSum)
return res;
}
return res;
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 3, 1, 7, 1 };
int n = arr.length;
System.out.println(minElements(arr, n));
}
}
// This code is contributed by Gitanjali
# Python3 code to find minimum number of
# elements such that their sum is greater
# than sum of remaining elements of the array.
# function to find minimum elements needed.
def minElements(arr, n):
# calculating HALF of array sum
halfSum = 0
for i in range(n):
halfSum = halfSum + arr[i]
halfSum = int(halfSum / 2)
# sort the array in descending order.
arr.sort(reverse=True)
res = 0
curr_sum = 0
for i in range(n):
curr_sum += arr[i]
res += 1
# current sum greater than sum
if curr_sum > halfSum:
return res
return res
# driver code
arr = [3, 1, 7, 1]
n = len(arr)
print(minElements(arr, n))
# This code is contributed by "Sharad_Bhardwaj".
// C# code to find minimum number of elements
// such that their sum is greater than sum of
// remaining elements of the array.
using System;
class GFG {
// Function to find minimum elements needed
static int minElements(int[] arr, int n)
{
// Calculating HALF of array sum
int halfSum = 0;
for (int i = 0; i < n; i++)
halfSum = halfSum + arr[i];
halfSum = halfSum / 2;
// Sort the array in ascending order and
// start traversing array from the ascending
// sort in descending order.
Array.Sort(arr);
int res = 0, curr_sum = 0;
for (int i = n - 1; i >= 0; i--) {
curr_sum += arr[i];
res++;
// Current sum greater than sum
if (curr_sum > halfSum)
return res;
}
return res;
}
// Driver Code
public static void Main()
{
int[] arr = { 3, 1, 7, 1 };
int n = arr.Length;
Console.WriteLine(minElements(arr, n));
}
}
// This code is contributed by vt_m.
<script>
// Javascript program to find minimum number of
// elements such that their sum is greater
// than sum of remaining elements of the array.
// function to find minimum elements needed.
function minElements(arr, n)
{
// calculating HALF of array sum
let halfSum = 0;
for (let i = 0; i < n; i++)
halfSum = halfSum + arr[i];
halfSum = parseInt(halfSum / 2, 10);
// sort the array in descending order.
arr.sort(function(a, b){return a - b});
arr.reverse();
let res = 0, curr_sum = 0;
for (let i = 0; i < n; i++) {
curr_sum += arr[i];
res++;
// current sum greater than sum
if (curr_sum > halfSum)
return res;
}
return res;
}
let arr = [3, 1, 7, 1];
let n = arr.length;
document.write(minElements(arr, n));
// This code is contributed by divyeshrabadiya07.
</script>
<?php
// PHP program to find minimum number
// of elements such that their sum is
// greater than sum of remaining
// elements of the array.
// function to find minimum elements needed.
function minElements($arr, $n)
{
// calculating HALF of array sum
$halfSum = 0;
for ($i = 0; $i < $n; $i++)
$halfSum = $halfSum + $arr[$i];
$halfSum = $halfSum / 2;
// sort the array in descending order.
rsort($arr);
$res = 0;
$curr_sum = 0;
for ($i = 0; $i < $n; $i++)
{
$curr_sum += $arr[$i];
$res++;
// current sum greater than sum
if ($curr_sum > $halfSum)
return $res;
}
return $res;
}
// Driver Code
$arr = array(3, 1, 7, 1);
$n = sizeof($arr);
echo minElements($arr, $n);
// This code is contributed by ihritik
?>
Output
1