text: Maximize count of elements that can be selected having minimum difference between their sum and K | C # implementation of the above approach ; Function to count maximum number of elements that can be selected ; Sorting the array ; Traverse the array ; Add the current element to the sum ; If sum exceeds k ; Increment count ; Returning the count ; Driver code ; Function call
code: using System ; class GFG { static int CountMaximum ( int [ ] arr , int n , int k ) { Array . Sort ( arr ) ; int sum = 0 , count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; if ( sum > k ) break ; count ++ ; } return count ; } static public void Main ( ) { int [ ] arr = new int [ ] { 30 , 30 , 10 , 10 } ; int n = 4 ; int k = 50 ; Console . WriteLine ( CountMaximum ( arr , n , k ) ) ; } }