VOOZH about

URL: https://www.geeksforgeeks.org/dsa/smallest-subset-sum-greater-elements/

⇱ Smallest subset sum greater than all others - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Smallest subset sum greater than all others

Last Updated : 25 Oct, 2024

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.

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 array

Input:  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.


Output
1


Comment
Article Tags: