Check if the sum of distinct digits of two integers are equal | C ++ program to check if the sum of distinct digits of two integers are equal ; Function to return the sum of distinct digits of a number ; Take last digit ; If digit has not been used before ; Set digit as used ; Remove last digit ; Function to check whether the sum of distinct digits of two numbers are equal ; Driver code
#include <iostream> NEW_LINE using namespace std ; int distinctDigitSum ( int n ) { bool used [ 10 ] ; int sum = 0 ; while ( n > 0 ) { int digit = n % 10 ; if ( ! used [ digit ] ) { used [ digit ] = true ; sum += digit ; } n = ( int ) n / 10 ; } return sum ; } string checkSum ( int m , int n ) { int sumM = distinctDigitSum ( m ) ; int sumN = distinctDigitSum ( n ) ; if ( sumM != sumN ) return " YES " ; return " NO " ; } int main ( ) { int m = 2452 , n = 9222 ; cout << ( checkSum ( m , n ) ) ; return 0 ; }