Count the maximum number of elements that can be selected from the array | C # implementation of the approach ; Function to return the maximum count of selection possible from the given array following the given process ; Initialize result ; Sorting the array ; Initialize the select variable ; Loop through array ; If selection is possible ; Increment result ; Increment selection variable ; Driver Code
using System ; class GFG { static int maxSelectionCount ( int [ ] a , int n ) { int res = 0 ; Array . Sort ( a ) ; int select = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] >= select ) { res ++ ; select ++ ; } } return res ; } public static void Main ( ) { int [ ] arr = { 4 , 2 , 1 , 3 , 5 , 1 , 4 } ; int N = arr . Length ; Console . WriteLine ( maxSelectionCount ( arr , N ) ) ; } }