Check if the sum of distinct digits of two integers are equal
Last Updated :
22 Aug, 2022
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++
#include <iostream>
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;
}
|
Java
public class HelloWorld {
static int distinctDigitSum( int n)
{
boolean used[] = new boolean [ 10 ];
int sum = 0 ;
while (n > 0 ) {
int digit = n % 10 ;
if (!used[digit]) {
used[digit] = true ;
sum += digit;
}
n = n / 10 ;
}
return sum;
}
static String checkSum( int m, int n)
{
int sumM = distinctDigitSum(m);
int sumN = distinctDigitSum(n);
if (sumM == sumN)
return "YES" ;
return "NO" ;
}
public static void main(String[] args)
{
int m = 2452 , n = 9222 ;
System.out.println(checkSum(m, n));
}
}
|
Python3
def distinctDigitSum(n) :
used = [ False ] * 10
sum = 0
while (n > 0 ) :
digit = n % 10
if ( not used[digit]) :
used[digit] = True
sum + = digit
n = n / / 10
return sum
def checkSum(m, n) :
sumM = distinctDigitSum(m)
sumN = distinctDigitSum(n)
if (sumM = = sumN) :
return "YES"
return "NO"
if __name__ = = "__main__" :
m = 2452
n = 9222
print (checkSum(m, n))
|
C#
using System;
public class GFG{
static int distinctDigitSum( int n)
{
bool []used = new bool [10];
int sum = 0;
while (n > 0) {
int digit = n % 10;
if (!used[digit]) {
used[digit] = true ;
sum += digit;
}
n = n / 10;
}
return sum;
}
static String checkSum( int m, int n)
{
int sumM = distinctDigitSum(m);
int sumN = distinctDigitSum(n);
if (sumM == sumN)
return "YES" ;
return "NO" ;
}
static public void Main (){
int m = 2452, n = 9222;
Console.WriteLine(checkSum(m, n));
}
}
|
PHP
<?php
function distinctDigitSum( $n )
{
$used [10] = array ();
$sum = 0;
while ( $n > 0)
{
$digit = $n % 10;
if ( $used > 0)
{
$used [ $digit ] = true;
$sum += $digit ;
}
$n = (int) $n / 10;
}
return $sum ;
}
function checkSum( $m , $n )
{
$sumM = distinctDigitSum( $m );
$sumN = distinctDigitSum( $n );
if ( $sumM != $sumN )
return "YES" ;
return "NO" ;
}
$m = 2452;
$n = 9222;
echo (checkSum( $m , $n ));
?>
|
Javascript
<script>
function distinctDigitSum(n)
{
var used = Array(10).fill( false );
var sum = 0;
while (n > 0)
{
var digit = n % 10;
if (!used[digit]) {
used[digit] = true ;
sum += digit;
}
n = parseInt(n / 10);
}
return sum;
}
function checkSum(m , n) {
var sumM = distinctDigitSum(m);
var sumN = distinctDigitSum(n);
if (sumM == sumN)
return "YES" ;
return "NO" ;
}
var m = 2452, n = 9222;
document.write(checkSum(m, n));
</script>
|
Time Complexity: O(log10m + log10n) since at the end of the loop the number gets divided by 10 so the algorithm takes logarithmic time to perform all operations
Auxiliary Space: O(10) since an array of constant length 10 is used so overall space occupied by the algorithm is constant
Share your thoughts in the comments
Please Login to comment...