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
def SumofDigits ( digit ) : NEW_LINE INDENT sum = 0 NEW_LINE while ( digit != 0 ) : NEW_LINE INDENT rem = digit % 10 NEW_LINE sum += rem NEW_LINE digit //= 10 NEW_LINE DEDENT return sum NEW_LINE DEDENT def findCountofNumbers ( arr , n , M ) : NEW_LINE INDENT SumDigits = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT s = SumofDigits ( arr [ i ] ) NEW_LINE SumDigits . append ( s ) NEW_LINE DEDENT SumDigits . sort ( ) NEW_LINE ip = list ( set ( SumDigits ) ) NEW_LINE count = 0 NEW_LINE sum = 0 NEW_LINE for i in range ( len ( SumDigits ) ) : NEW_LINE INDENT if ( sum > M ) : NEW_LINE INDENT break NEW_LINE DEDENT sum += SumDigits [ i ] NEW_LINE if ( sum <= M ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 45 , 16 , 17 , 219 , 32 , 22 ] NEW_LINE M = 10 NEW_LINE n = len ( arr ) NEW_LINE print ( findCountofNumbers ( arr , n , M ) ) NEW_LINE DEDENT