Given an array of non-negative integers. Our task is to find minimum number of elements such that their sum should be greater than the sum of rest of the elements of the array.
Examples :
Input : arr[] = {3, 1, 7, 1}
Output : 1
Smallest subset is {7}. Sum of
this subset is greater than all
other elements {3, 1, 1}
Input : arr[] = {2, 1, 2}
Output : 2
In this example one element is not
enough. We can pick elements with
values 1, 2 or 2, 2. In any case,
the minimum count is 2.
The Brute force approach is to find the sum of all the possible subsets and then compare sum with the sum of remaining elements.
The Efficient Approach is to take the largest elements. We sort values in descending order, then take elements from the largest, until we get strictly more than half of total sum of the given array.
C++
// CPP program to find minimum number of // elements such that their sum is greater // than sum of remaining elements of the array. #include <bits/stdc++.h> #include <string.h> using namespace std; // function to find minimum elements needed. 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 descending order. sort(arr, arr + n, greater<int>()); int res = 0, curr_sum = 0; for (int i = 0; i < n; i++) { curr_sum += arr[i]; res++; // current sum greater than sum if (curr_sum > halfSum) return res; } return res; } // Driver function int main() { int arr[] = {3, 1, 7, 1}; int n = sizeof(arr) / sizeof(arr[0]); cout << minElements(arr, n) << endl; return 0; } |
Java
// 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
# 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#
// 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. |
PHP
<?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
Time Complexity : O(n Log n)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Length of longest subarray in which elements greater than K are more than elements not greater than K
- Smallest subarray of size greater than K with sum greater than a given value
- Count of elements whose absolute difference with the sum of all the other elements is greater than k
- Smallest subarray having an element with frequency greater than that of other elements
- Number of positions such that adding K to the element is greater than sum of all other elements
- Largest possible Subset from an Array such that no element is K times any other element in the Subset
- Longest subsequence with first and last element greater than all other elements
- Sum of all array elements less than X and greater than Y for Q queries
- Maximum number of elements greater than X after equally distributing subset of array
- Count of Array elements greater than all elements on its left and at least K elements on its right
- Count of Array elements greater than all elements on its left and next K elements on its right
- Smallest subarray such that all elements are greater than K
- Sum of maximum and minimum of Kth subset ordered by increasing subset sum
- Length of Smallest Subsequence such that sum of elements is greater than equal to K
- Merge first two minimum elements of the array until all the elements are greater than K
- Reduce the array by deleting elements which are greater than all elements to its left
- Count of array elements which are greater than all elements on its left
- Size of smallest subarray to be removed to make count of array elements greater and smaller than K equal
- Find the element before which all the elements are smaller than it, and after which all are greater
- Find K such that changing all elements of the Array greater than K to K will make array sum N
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

