Check if two numbers have same digits
The given problem is to determine whether two given integers have the same length or not. The length of an integer is defined as the number of digits it contains. For example, the integer 12345 has a length of 5, and the integer -5678 has a length of 4.
Problem Statement
The task is to write a C program that takes two integers as input and checks if they have the same length or not. The program should print "Same length" if both integers have the same number of digits and "Not same length" if they have different lengths.
Example
Let's take a few examples to understand the problem better.
-
Input: a = 2311, b = 1221 Output: Same length Explanation: Both integers have four digits, so they have the same length.
-
Input: a = 1311, b = 111 Output: Not same length Explanation: The first integer has four digits, while the second integer has only three digits, so they have different lengths.
-
Input: a = 1321, b = -1113 Output: Same length Explanation: Both integers have four digits, so they have the same length. The absolute value of negative integer -1113 is 1113, which still has four digits.
Standard Pseudocode
Below is the standard pseudocode for the algorithm to check if two numbers have the same length:
function isSameDigit(a, b):
n = absValue(a)
count = array of size 10 initialized with zeros
# Count digit frequency of number A
while n > 0:
count[n % 10]++
n = n / 10
n = absValue(b)
# Reduce frequency digit of number B
while n > 0:
count[n % 10]--
n = n / 10
# Check if both numbers have the same digits
for i = 0 to 9:
if count[i] != 0:
return False
return True
Algorithm Explanation
-
Define a helper function
absValue(num)
to compute the absolute value of a given numbernum
. If the number is negative, return its negation; otherwise, return the number itself. -
Define the main function
isSameLength(a, b)
to check if two integersa
andb
have the same length or not. -
Initialize two variables
x
andy
to store the absolute values ofa
andb
, respectively. -
Use a while loop to reduce both
x
andy
by dividing them by 10 until they become 0 or less. This effectively counts the number of digits in each number. -
After the while loop, check if both
x
andy
are 0. If they are, it means botha
andb
have the same number of digits, and we print "Same length." Otherwise, we print "Not same length." -
In the main function, call the
isSameLength()
function with three different pairs of integers to test the algorithm.
Code Solution
Here given code implementation process.
// C Program for
// Check if two numbers have same digits
#include <stdio.h>
int absValue(int num)
{
if (num < 0)
{
return -num;
}
return num;
}
void isSameDigit(int a, int b)
{
int n = absValue(a);
int count[10];
for (int i = 0; i < 10; ++i)
{
count[i] = 0;
}
// Count digit frequency of number A
while (n > 0)
{
count[n % 10]++;
n = n / 10;
}
n = absValue(b);
// Reduce frequency digit of number B
while (n > 0)
{
count[n % 10]--;
n = n / 10;
}
printf("\n Given number a : %d b : %d", a, b);
for (int i = 0; i < 10; ++i)
{
if (count[i] != 0)
{
// When No
printf("\n Both not contain same digits");
return;
}
}
printf("\n Both contain same digits");
}
int main(int argc, char const *argv[])
{
// Test
isSameDigit(2311, 1321);
isSameDigit(1311, 111);
isSameDigit(1321, -2113);
return 0;
}
Output
Given number a : 2311 b : 1321
Both contain same digits
Given number a : 1311 b : 111
Both not contain same digits
Given number a : 1321 b : -2113
Both contain same digits
// Java program for
// Check if two numbers have same digits
public class EqualDigits
{
public int absValue(int num)
{
if (num < 0)
{
return -num;
}
return num;
}
public void isSameDigit(int a, int b)
{
int n = absValue(a);
int[] count = new int[10];
for (int i = 0; i < 10; ++i)
{
count[i] = 0;
}
// Count digit frequency of number A
while (n > 0)
{
count[n % 10]++;
n = n / 10;
}
n = absValue(b);
// Reduce frequency digit of number B
while (n > 0)
{
count[n % 10]--;
n = n / 10;
}
System.out.print("\n Given number a : " + a + " b : " + b);
for (int i = 0; i < 10; ++i)
{
if (count[i] != 0)
{
// When No
System.out.print("\n Both not contain same digits");
return;
}
}
System.out.print("\n Both contain same digits");
}
public static void main(String[] args)
{
EqualDigits task = new EqualDigits();
// Test
task.isSameDigit(2311, 1321);
task.isSameDigit(1311, 111);
task.isSameDigit(1321, -2113);
}
}
Output
Given number a : 2311 b : 1321
Both contain same digits
Given number a : 1311 b : 111
Both not contain same digits
Given number a : 1321 b : -2113
Both contain same digits
// Include header file
#include <iostream>
using namespace std;
// C++ program for
// Check if two numbers have same digits
class EqualDigits
{
public: int absValue(int num)
{
if (num < 0)
{
return -num;
}
return num;
}
void isSameDigit(int a, int b)
{
int n = this->absValue(a);
int count[10];
for (int i = 0; i < 10; ++i)
{
count[i] = 0;
}
// Count digit frequency of number A
while (n > 0)
{
count[n % 10]++;
n = n / 10;
}
n = this->absValue(b);
// Reduce frequency digit of number B
while (n > 0)
{
count[n % 10]--;
n = n / 10;
}
cout << "\n Given number a : " << a << " b : " << b;
for (int i = 0; i < 10; ++i)
{
if (count[i] != 0)
{
// When No
cout << "\n Both not contain same digits";
return;
}
}
cout << "\n Both contain same digits";
}
};
int main()
{
EqualDigits *task = new EqualDigits();
// Test
task->isSameDigit(2311, 1321);
task->isSameDigit(1311, 111);
task->isSameDigit(1321, -2113);
return 0;
}
Output
Given number a : 2311 b : 1321
Both contain same digits
Given number a : 1311 b : 111
Both not contain same digits
Given number a : 1321 b : -2113
Both contain same digits
package main
import "fmt"
// Go program for
// Check if two numbers have same digits
type EqualDigits struct {}
func getEqualDigits() * EqualDigits {
var me *EqualDigits = &EqualDigits {}
return me
}
func(this EqualDigits) absValue(num int) int {
if num < 0 {
return -num
}
return num
}
func(this EqualDigits) isSameDigit(a, b int) {
var n int = this.absValue(a)
var count = make([] int, 10)
for i := 0 ; i < 10 ; i++ {
count[i] = 0
}
// Count digit frequency of number A
for (n > 0) {
count[n % 10]++
n = n / 10
}
n = this.absValue(b)
// Reduce frequency digit of number B
for (n > 0) {
count[n % 10]--
n = n / 10
}
fmt.Print("\n Given number a : ", a, " b : ", b)
for i := 0 ; i < 10 ; i++ {
if count[i] != 0 {
// When No
fmt.Print("\n Both not contain same digits")
return
}
}
fmt.Print("\n Both contain same digits")
}
func main() {
var task * EqualDigits = getEqualDigits()
// Test
task.isSameDigit(2311, 1321)
task.isSameDigit(1311, 111)
task.isSameDigit(1321, -2113)
}
Output
Given number a : 2311 b : 1321
Both contain same digits
Given number a : 1311 b : 111
Both not contain same digits
Given number a : 1321 b : -2113
Both contain same digits
// Include namespace system
using System;
// Csharp program for
// Check if two numbers have same digits
public class EqualDigits
{
public int absValue(int num)
{
if (num < 0)
{
return -num;
}
return num;
}
public void isSameDigit(int a, int b)
{
int n = this.absValue(a);
int[] count = new int[10];
for (int i = 0; i < 10; ++i)
{
count[i] = 0;
}
// Count digit frequency of number A
while (n > 0)
{
count[n % 10]++;
n = n / 10;
}
n = this.absValue(b);
// Reduce frequency digit of number B
while (n > 0)
{
count[n % 10]--;
n = n / 10;
}
Console.Write("\n Given number a : " + a + " b : " + b);
for (int i = 0; i < 10; ++i)
{
if (count[i] != 0)
{
// When No
Console.Write("\n Both not contain same digits");
return;
}
}
Console.Write("\n Both contain same digits");
}
public static void Main(String[] args)
{
EqualDigits task = new EqualDigits();
// Test
task.isSameDigit(2311, 1321);
task.isSameDigit(1311, 111);
task.isSameDigit(1321, -2113);
}
}
Output
Given number a : 2311 b : 1321
Both contain same digits
Given number a : 1311 b : 111
Both not contain same digits
Given number a : 1321 b : -2113
Both contain same digits
<?php
// Php program for
// Check if two numbers have same digits
class EqualDigits
{
public function absValue($num)
{
if ($num < 0)
{
return -$num;
}
return $num;
}
public function isSameDigit($a, $b)
{
$n = $this->absValue($a);
$count = array_fill(0, 10, 0);
// Count digit frequency of number A
while ($n > 0)
{
$count[$n % 10]++;
$n = (int)($n / 10);
}
$n = $this->absValue($b);
// Reduce frequency digit of number B
while ($n > 0)
{
$count[$n % 10]--;
$n = (int)($n / 10);
}
echo("\n Given number a : ".$a.
" b : ".$b);
for ($i = 0; $i < 10; ++$i)
{
if ($count[$i] != 0)
{
// When No
echo("\n Both not contain same digits");
return;
}
}
echo("\n Both contain same digits");
}
}
function main()
{
$task = new EqualDigits();
// Test
$task->isSameDigit(2311, 1321);
$task->isSameDigit(1311, 111);
$task->isSameDigit(1321, -2113);
}
main();
Output
Given number a : 2311 b : 1321
Both contain same digits
Given number a : 1311 b : 111
Both not contain same digits
Given number a : 1321 b : -2113
Both contain same digits
// Node JS program for
// Check if two numbers have same digits
class EqualDigits
{
absValue(num)
{
if (num < 0)
{
return -num;
}
return num;
}
isSameDigit(a, b)
{
var n = this.absValue(a);
var count = Array(10).fill(0);
// Count digit frequency of number A
while (n > 0)
{
count[n % 10]++;
n = parseInt(n / 10);
}
n = this.absValue(b);
// Reduce frequency digit of number B
while (n > 0)
{
count[n % 10]--;
n = parseInt(n / 10);
}
process.stdout.write("\n Given number a : " + a + " b : " + b);
for (var i = 0; i < 10; ++i)
{
if (count[i] != 0)
{
// When No
process.stdout.write("\n Both not contain same digits");
return;
}
}
process.stdout.write("\n Both contain same digits");
}
}
function main()
{
var task = new EqualDigits();
// Test
task.isSameDigit(2311, 1321);
task.isSameDigit(1311, 111);
task.isSameDigit(1321, -2113);
}
main();
Output
Given number a : 2311 b : 1321
Both contain same digits
Given number a : 1311 b : 111
Both not contain same digits
Given number a : 1321 b : -2113
Both contain same digits
# Python 3 program for
# Check if two numbers have same digits
class EqualDigits :
def absValue(self, num) :
if (num < 0) :
return -num
return num
def isSameDigit(self, a, b) :
n = self.absValue(a)
count = [0] * (10)
# Count digit frequency of number A
while (n > 0) :
count[n % 10] += 1
n = int(n / 10)
n = self.absValue(b)
# Reduce frequency digit of number B
while (n > 0) :
count[n % 10] -= 1
n = int(n / 10)
print("\n Given number a : ", a ," b : ", b, end = "")
i = 0
while (i < 10) :
if (count[i] != 0) :
# When No
print("\n Both not contain same digits", end = "")
return
i += 1
print("\n Both contain same digits", end = "")
def main() :
task = EqualDigits()
# Test
task.isSameDigit(2311, 1321)
task.isSameDigit(1311, 111)
task.isSameDigit(1321, -2113)
if __name__ == "__main__": main()
Output
Given number a : 2311 b : 1321
Both contain same digits
Given number a : 1311 b : 111
Both not contain same digits
Given number a : 1321 b : -2113
Both contain same digits
# Ruby program for
# Check if two numbers have same digits
class EqualDigits
def absValue(num)
if (num < 0)
return -num
end
return num
end
def isSameDigit(a, b)
n = self.absValue(a)
count = Array.new(10) {0}
# Count digit frequency of number A
while (n > 0)
count[n % 10] += 1
n = n / 10
end
n = self.absValue(b)
# Reduce frequency digit of number B
while (n > 0)
count[n % 10] -= 1
n = n / 10
end
print("\n Given number a : ", a ," b : ", b)
i = 0
while (i < 10)
if (count[i] != 0)
# When No
print("\n Both not contain same digits")
return
end
i += 1
end
print("\n Both contain same digits")
end
end
def main()
task = EqualDigits.new()
# Test
task.isSameDigit(2311, 1321)
task.isSameDigit(1311, 111)
task.isSameDigit(1321, -2113)
end
main()
Output
Given number a : 2311 b : 1321
Both contain same digits
Given number a : 1311 b : 111
Both not contain same digits
Given number a : 1321 b : -2113
Both contain same digits
// Scala program for
// Check if two numbers have same digits
class EqualDigits()
{
def absValue(num: Int): Int = {
if (num < 0)
{
return -num;
}
return num;
}
def isSameDigit(a: Int, b: Int): Unit = {
var n: Int = absValue(a);
var count: Array[Int] = Array.fill[Int](10)(0);
// Count digit frequency of number A
while (n > 0)
{
count(n % 10) += 1;
n = n / 10;
}
n = absValue(b);
// Reduce frequency digit of number B
while (n > 0)
{
count(n % 10) -= 1;
n = n / 10;
}
print("\n Given number a : " + a + " b : " + b);
var i: Int = 0;
while (i < 10)
{
if (count(i) != 0)
{
// When No
print("\n Both not contain same digits");
return;
}
i += 1;
}
print("\n Both contain same digits");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: EqualDigits = new EqualDigits();
// Test
task.isSameDigit(2311, 1321);
task.isSameDigit(1311, 111);
task.isSameDigit(1321, -2113);
}
}
Output
Given number a : 2311 b : 1321
Both contain same digits
Given number a : 1311 b : 111
Both not contain same digits
Given number a : 1321 b : -2113
Both contain same digits
// Swift 4 program for
// Check if two numbers have same digits
class EqualDigits
{
func absValue(_ num: Int) -> Int
{
if (num < 0)
{
return -num;
}
return num;
}
func isSameDigit(_ a: Int, _ b: Int)
{
var n: Int = self.absValue(a);
var count: [Int] = Array(repeating: 0, count: 10);
// Count digit frequency of number A
while (n > 0)
{
count[n % 10] += 1;
n = n / 10;
}
n = self.absValue(b);
// Reduce frequency digit of number B
while (n > 0)
{
count[n % 10] -= 1;
n = n / 10;
}
print("\n Given number a : ", a ," b : ", b, terminator: "");
var i: Int = 0;
while (i < 10)
{
if (count[i] != 0)
{
// When No
print("\n Both not contain same digits", terminator: "");
return;
}
i += 1;
}
print("\n Both contain same digits", terminator: "");
}
}
func main()
{
let task: EqualDigits = EqualDigits();
// Test
task.isSameDigit(2311, 1321);
task.isSameDigit(1311, 111);
task.isSameDigit(1321, -2113);
}
main();
Output
Given number a : 2311 b : 1321
Both contain same digits
Given number a : 1311 b : 111
Both not contain same digits
Given number a : 1321 b : -2113
Both contain same digits
// Kotlin program for
// Check if two numbers have same digits
class EqualDigits
{
fun absValue(num: Int): Int
{
if (num < 0)
{
return -num;
}
return num;
}
fun isSameDigit(a: Int, b: Int): Unit
{
var n: Int = this.absValue(a);
val count: Array < Int > = Array(10)
{
0
};
// Count digit frequency of number A
while (n > 0)
{
count[n % 10] += 1;
n = n / 10;
}
n = this.absValue(b);
// Reduce frequency digit of number B
while (n > 0)
{
count[n % 10] -= 1;
n = n / 10;
}
print("\n Given number a : " + a + " b : " + b);
var i: Int = 0;
while (i < 10)
{
if (count[i] != 0)
{
// When No
print("\n Both not contain same digits");
return;
}
i += 1;
}
print("\n Both contain same digits");
}
}
fun main(args: Array < String > ): Unit
{
val task: EqualDigits = EqualDigits();
// Test
task.isSameDigit(2311, 1321);
task.isSameDigit(1311, 111);
task.isSameDigit(1321, -2113);
}
Output
Given number a : 2311 b : 1321
Both contain same digits
Given number a : 1311 b : 111
Both not contain same digits
Given number a : 1321 b : -2113
Both contain same digits
Resultant Output Explanation
The output of the given C program for the provided test cases is as follows:
-
Given number a: 2311, b: 1221 Output: Same length Explanation: Both numbers have four digits, so they have the same length.
-
Given number a: 1311, b: 111 Output: Not same length Explanation: The first number has four digits, while the second number has three digits, so they have different lengths.
-
Given number a: 1321, b: -1113 Output: Same length Explanation: The absolute value of the negative number -1113 is 1113, which still has four digits. Both numbers have four digits, so they have the same length.
Time Complexity:
The time complexity of the given algorithm is O(log10(max(a, b))). The while loop runs until both x
and
y
become 0 or less, and in each iteration, we divide x
and y
by 10. The
number of iterations required to reach 0 or less is equal to the number of digits in the larger of the two numbers
(a
or b
). The number of digits in a number is given by log10(number), so the overall time
complexity is logarithmic in the larger number.
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment