text: Smallest subset with sum greater than all other elements | C # 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 ; Calculating HALF of array sum ; Sort the array in ascending order and start traversing array from the ascending sort in descending order . ; Current sum greater than sum ; Driver Code
code: 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 ) ) ; } }