Check if the sum of distinct digits of two integers are equal

Given two integer m and n, the task is to find the sum of distinct digits of both the numbers and print YES if the both the sums are equal else print NO.

Examples:

Input: m = 2452, n = 9222
Output: YES
The sum of distinct digits of 2452 is 11 (2 + 4 + 5)
And of 9222 is 11 (9 + 2)

Input: m = 121, n = 3035
Output: NO

Approach: Find the sum of unique digits of m and n and store them in sumM and sumN respectively. If sumM = sumN then print YES else print NO.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to check if the sum of distinct 
// digits of two integers are equal
public class HelloWorld {
  
    // Function to return the sum of
    // distinct digits of a number
    static int distinctDigitSum(int n)
    {
        boolean used[] = new boolean[10];
        int sum = 0;
        while (n > 0) {
  
            // Take last digit
            int digit = n % 10;
  
            // If digit has not been used before
            if (!used[digit]) {
  
                // Set digit as used
                used[digit] = true;
                sum += digit;
            }
  
            // Remove last digit
            n = n / 10;
        }
  
        return sum;
    }
  
    // Function to check whether the sum of
    // distinct digits of two numbers are equal
    static String checkSum(int m, int n)
    {
        int sumM = distinctDigitSum(m);
        int sumN = distinctDigitSum(n);
  
        if (sumM == sumN)
            return "YES";
        return "NO";
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int m = 2452, n = 9222;
        System.out.println(checkSum(m, n));
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 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 
def distinctDigitSum(n) :
      
    used = [False] * 10
    sum = 0
    while (n > 0) :
  
        # Take last digit 
        digit = n % 10
          
        # If digit has not been used before 
        if (not used[digit]) : 
  
            # Set digit as used 
            used[digit] = True
            sum += digit
              
        # Remove last digit 
        n = n // 10
          
    return sum
      
# Function to check whether the sum of 
# distinct digits of two numbers are equal 
def checkSum(m, n) :
      
    sumM = distinctDigitSum(m) 
    sumN = distinctDigitSum(n) 
      
    if (sumM == sumN) :
        return "YES"
    return "NO"
      
# Driver code 
if __name__ == "__main__" :
  
    m = 2452
    n = 9222
      
    print(checkSum(m, n))
      
# This code is contributed by Ryuga

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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
  
using System;
  
public class GFG{
        static int distinctDigitSum(int n)
    {
        bool []used = new bool[10];
        int sum = 0;
        while (n > 0) {
  
            // Take last digit
            int digit = n % 10;
  
            // If digit has not been used before
            if (!used[digit]) {
  
                // Set digit as used
                used[digit] = true;
                sum += digit;
            }
  
            // Remove last digit
            n = n / 10;
        }
  
        return sum;
    }
  
    // Function to check whether the sum of
    // distinct digits of two numbers are equal
    static String checkSum(int m, int n)
    {
        int sumM = distinctDigitSum(m);
        int sumN = distinctDigitSum(n);
  
        if (sumM == sumN)
            return "YES";
        return "NO";
    }
  
    // Driver code
    static public void Main (){
        int m = 2452, n = 9222;
        Console.WriteLine(checkSum(m, n));
    }
//This code is contributed by akt_mit    
}

chevron_right


PHP

0)
{

// Take last digit
$digit = $n % 10;

// If digit has not been used before
if ($used > 0)
{

// Set digit as used
$used[$digit] = true;
$sum += $digit;
}

// Remove last digit
$n = (int)$n / 10;
}

return $sum;
}

// Function to check whether the sum of
// distinct digits of two numbers are equal
function checkSum($m, $n)
{
$sumM = distinctDigitSum($m);
$sumN = distinctDigitSum($n);

if ($sumM != $sumN)
return “YES”;
return “NO”;
}

// Driver code
$m = 2452;
$n = 9222;
echo (checkSum($m, $n));

// This code is contributed by ajit..
?>

Output:

YES


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : jit_t, AnkitRai01