Count numbers whose maximum sum of distinct digit | Function to find the digit - sum of a number ; Loop to iterate the number digit - wise to find digit - sum ; Variable to store last digit ; Function to find the count of number ; Vector to store the Sum of Digits ; Sum of digits for each element in vector ; Sorting the digitSum vector ; Removing the duplicate elements ; Count variable to store the Count ; Finding the Count of Numbers ; Driver Code ; Function Call
function SumofDigits ( digit ) { let sum = 0 ; while ( digit != 0 ) { let rem = digit % 10 ; sum += rem ; digit = Math . floor ( digit / 10 ) ; } return sum ; } function findCountofNumbers ( arr , n , M ) { let SumDigits = [ ] ; for ( let i = 0 ; i < n ; i ++ ) { let s = SumofDigits ( arr [ i ] ) ; SumDigits . push ( s ) ; } SumDigits . sort ( function ( a , b ) { return a - b ; } ) ; let ip = Array . from ( new Set ( SumDigits ) ) ; let count = 0 ; let sum = 0 ; for ( let i = 0 ; i < SumDigits . length ; i ++ ) { if ( sum > M ) break ; sum += SumDigits [ i ] ; if ( sum <= M ) count ++ ; } return count ; } let arr = [ 1 , 45 , 16 , 17 , 219 , 32 , 22 ] ; let M = 10 ; let n = arr . length ; document . write ( findCountofNumbers ( arr , n , M ) ) ;