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 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
Smallest subset with sum greater than all other elements using Sorting
The approach is to take the largest elements from the array , in that way we can decrease the size of the subset that has sum greater than the sum of rest of the elements of the array , so we sort the array in descending order, then take the largest elements, until we get strictly more than half of total sum of the given array.
Follow the steps mentioned below to implement the idea:
- Create a variable halfSum to store half of the overall sum of the array arr[].
- Sort the array in descending order.
- Create a variable curr_sum and increase the value of curr_sum by arr[i] while traversing the array from index 0 till the value of curr_sum is less than halfSum
- When curr_sum is greater than halfSum return index+1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#include <string.h>
using namespace std;
int minElements( int arr[], int n)
{
int halfSum = 0;
for ( int i = 0; i < n; i++)
halfSum = halfSum + arr[i];
halfSum = halfSum / 2;
sort(arr, arr + n, greater< int >());
int res = 0, curr_sum = 0;
for ( int i = 0; i < n; i++) {
curr_sum += arr[i];
res++;
if (curr_sum > halfSum)
return res;
}
return res;
}
int main()
{
int arr[] = { 3, 1, 7, 1 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << minElements(arr, n) << endl;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static int minElements( int arr[], int n)
{
int halfSum = 0 ;
for ( int i = 0 ; i < n; i++)
halfSum = halfSum + arr[i];
halfSum = halfSum / 2 ;
Arrays.sort(arr);
int res = 0 , curr_sum = 0 ;
for ( int i = n - 1 ; i >= 0 ; i--) {
curr_sum += arr[i];
res++;
if (curr_sum > halfSum)
return res;
}
return res;
}
public static void main(String[] args)
{
int arr[] = { 3 , 1 , 7 , 1 };
int n = arr.length;
System.out.println(minElements(arr, n));
}
}
|
Python3
def minElements(arr, n):
halfSum = 0
for i in range (n):
halfSum = halfSum + arr[i]
halfSum = int (halfSum / 2 )
arr.sort(reverse = True )
res = 0
curr_sum = 0
for i in range (n):
curr_sum + = arr[i]
res + = 1
if curr_sum > halfSum:
return res
return res
arr = [ 3 , 1 , 7 , 1 ]
n = len (arr)
print (minElements(arr, n))
|
C#
using System;
class GFG {
static int minElements( int [] arr, int n)
{
int halfSum = 0;
for ( int i = 0; i < n; i++)
halfSum = halfSum + arr[i];
halfSum = halfSum / 2;
Array.Sort(arr);
int res = 0, curr_sum = 0;
for ( int i = n - 1; i >= 0; i--) {
curr_sum += arr[i];
res++;
if (curr_sum > halfSum)
return res;
}
return res;
}
public static void Main()
{
int [] arr = { 3, 1, 7, 1 };
int n = arr.Length;
Console.WriteLine(minElements(arr, n));
}
}
|
PHP
<?php
function minElements( $arr , $n )
{
$halfSum = 0;
for ( $i = 0; $i < $n ; $i ++)
$halfSum = $halfSum + $arr [ $i ];
$halfSum = $halfSum / 2;
rsort( $arr );
$res = 0;
$curr_sum = 0;
for ( $i = 0; $i < $n ; $i ++)
{
$curr_sum += $arr [ $i ];
$res ++;
if ( $curr_sum > $halfSum )
return $res ;
}
return $res ;
}
$arr = array (3, 1, 7, 1);
$n = sizeof( $arr );
echo minElements( $arr , $n );
?>
|
Javascript
<script>
function minElements(arr, n)
{
let halfSum = 0;
for (let i = 0; i < n; i++)
halfSum = halfSum + arr[i];
halfSum = parseInt(halfSum / 2, 10);
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++;
if (curr_sum > halfSum)
return res;
}
return res;
}
let arr = [3, 1, 7, 1];
let n = arr.length;
document.write(minElements(arr, n));
</script>
|
Time Complexity: O(N*log(N)).
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
22 Aug, 2022
Like Article
Save Article