Count the maximum number of elements that can be selected from the array | 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
function maxSelectionCount ( a , n ) { var res = 0 ; a . sort ( ) ; var select = 1 ; for ( var i = 0 ; i < n ; i ++ ) { if ( a [ i ] >= select ) { res ++ ; select ++ ; } } return res ; } var arr = [ 4 , 2 , 1 , 3 , 5 , 1 , 4 ] ; var N = arr . length ; document . write ( maxSelectionCount ( arr , N ) ) ;