text: Maximize count of elements that can be selected having minimum difference between their sum and K | Function to count maximum number of elements that can be selected ; Sort the array ; Traverse the array ; Add current element to the sum ; IF sum exceeds k ; Increment count ; Return the count ;  ; Function call
code: function CountMaximum ( arr , n , k ) { arr . sort ( function ( a , b ) { return a - b } ) ; let sum = 0 , count = 0 ; for ( let i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; if ( sum > k ) break ; count ++ ; } return count ; } let arr = [ 30 , 30 , 10 , 10 ] ; let n = arr . length ; let k = 50 ; document . write ( CountMaximum ( arr , n , k ) ) ;