Check if given number is stable or unstable
The given problem is to check whether a given number is stable or unstable. A stable number is one where all the digits have the same frequency, while an unstable number has different frequencies for its digits. For example, the number 112233 is stable because the digit '1' appears twice, '2' appears twice, and '3' appears twice. On the other hand, the number 89198 is unstable because the digit '8' appears twice, '9' appears once, and '1' appears once.
Explanation with Suitable Example
Let's take the number 112233 to illustrate how the code works:
-
We initialize an array called
result
of size 10, where each index represents a digit (0 to 9). The purpose of this array is to store the frequency of each digit in the given number. -
The
frequency()
function is called, which calculates the frequency of digits in the number and updates theresult
array accordingly. For the number 112233, theresult
array would be:[0, 2, 2, 2, 0, 0, 0, 0, 0, 0]
. -
The
stable_no()
function then checks theresult
array to determine if the number is stable or unstable. It does this by comparing the frequency of each digit with the frequency of the first non-zero digit. In our example, the first non-zero digit is '2', and its frequency is 2. So, we compare the frequency of all other digits with 2. If any digit's frequency differs from 2, the number is considered unstable. -
In the example of 112233, the frequencies of all digits are 2, so the function determines that the number is stable and prints the output as "112233 Is an stable number."
Pseudocode
function frequency(number, result):
while number is not 0:
temp = number % 10
if temp < 0:
temp = -temp
result[temp]++
number = number / 10
function stable_no(number):
create an array result of size 10, initialized with zeros
auxiliary = 0
status = 1
call frequency(number, result)
for i from 0 to 9:
if result[i] is not 0:
if auxiliary is 0:
auxiliary = result[i]
else if auxiliary is not result[i]:
status = 0
break
if status is 0:
print number, "Is not a stable number"
else:
print number, "Is an stable number"
function main():
call stable_no(123)
call stable_no(1111)
call stable_no(89198)
call stable_no(112233)
call stable_no(10101)
Algorithm Explanation
-
The
frequency()
function takes an integernumber
and an arrayresult
as input. It calculates the frequency of digits innumber
and stores the frequencies in theresult
array. -
The
stable_no()
function takes an integernumber
as input. It initializes an arrayresult
of size 10 with zeros. It then calls thefrequency()
function to calculate the frequency of digits in thenumber
. -
The function
stable_no()
uses a variableauxiliary
to keep track of the frequency of the first non-zero digit encountered in theresult
array. -
It uses a variable
status
to keep track of whether the number is stable or not. The initial value ofstatus
is set to 1 (stable). -
The function then iterates through the
result
array. If it encounters a non-zero frequency, it checks ifauxiliary
is 0 (which means it's the first non-zero frequency encountered), and if so, it setsauxiliary
to that frequency. Ifauxiliary
is not 0, it compares the current frequency withauxiliary
. If they are different, it means the number is unstable, andstatus
is set to 0 (unstable). -
After the loop, the function checks the value of
status
. If it's 0, it prints that the number is not stable. Otherwise, it prints that the number is stable.
Code Solution
Here given code implementation process.
//C Program
//Check if given number is stable or unstable
#include <stdio.h>
//Calculate the frequency of digits in number
void frequency(int number, int result[])
{
int temp = 0;
while (number != 0)
{
temp = number % 10;
if (temp < 0)
{
//When get negative number
temp = -temp;
}
//modify the value of temp index element in result array
result[temp]++;
number /= 10;
}
}
void stable_no(int number)
{
//resultant array which is store the frequency of number digits
int result[10];
for (int i = 0; i < 10; ++i)
{
//Set the initial value of each element to zero
result[i] = 0;
}
int auxiliary = 0, status = 1;
//Calculate number digit frequency
frequency(number, result);
for (int i = 0; i < 10 && status == 1; ++i)
{
//Check that the resultant array
//[i] location frequency is greater than zero or not
if (result[i] != 0)
{
if (auxiliary == 0)
{
//get first number frequency
auxiliary = result[i];
}
else if (auxiliary != result[i])
{
//When no two numbers frequency are same in given number
//here, break the loop execution
status = 0;
}
}
}
if (status == 0)
{
printf("%d Is not a stable number\n", number);
}
else
{
printf("%d Is an stable number\n", number);
}
printf("\n");
}
int main()
{
//Test Case
stable_no(123);
stable_no(1111);
stable_no(89198);
stable_no(112233);
stable_no(10101);
return 0;
}
Output
123 Is an stable number
1111 Is an stable number
89198 Is not a stable number
112233 Is an stable number
10101 Is not a stable number
/*
C++ Program
Check if given number is stable or unstable
*/
#include<iostream>
using namespace std;
class MyNumber
{
public:
//Calculate the frequency of digits in number
void frequency(int number, int *result)
{
int temp = 0;
while (number != 0)
{
temp = number % 10;
if (temp < 0)
{
//When get negative number
temp = -temp;
}
//modify the value of temp index element in result array
result[temp]++;
number /= 10;
}
}
void stable_no(int number)
{
int *result = new int[10];
for (int i = 0; i < 10; ++i)
{
//Set the initial value of each element to zero
result[i] = 0;
}
int auxiliary = 0, status = 1;
//Calculate number digit frequency
this->frequency(number, result);
for (int i = 0; i < 10 && status == 1; ++i)
{
//Check that the resultant array
//[i] location frequency is greater than zero or not
if (result[i] != 0)
{
if (auxiliary == 0)
{
//get first number frequency
auxiliary = result[i];
}
else if (auxiliary != result[i])
{
//When no two numbers frequency are same in given number
//here, break the loop execution
status = 0;
}
}
}
if (status == 0)
{
cout << "" << number << " Is not a stable number\n";
}
else
{
cout << "" << number << " Is an stable number\n";
}
cout << "\n";
}
};
int main()
{
MyNumber obj = MyNumber();
//Test Case
obj.stable_no(123);
obj.stable_no(1111);
obj.stable_no(89198);
obj.stable_no(112233);
obj.stable_no(10101);
return 0;
}
Output
123 Is an stable number
1111 Is an stable number
89198 Is not a stable number
112233 Is an stable number
10101 Is not a stable number
/*
C# Program
Check if given number is stable or unstable
*/
using System;
public class MyNumber
{
//Calculate the frequency of digits in number
public void frequency(int number, int[] result)
{
int temp = 0;
while (number != 0)
{
temp = number % 10;
if (temp < 0)
{
//When get negative number
temp = -temp;
}
//modify the value of temp index element in result array
result[temp]++;
number /= 10;
}
}
public void stable_no(int number)
{
//resultant array which is store the frequency of number digits
int[] result = new int[10];
for (int i = 0; i < 10; i++)
{
//Set the initial value of each element to zero
result[i] = 0;
}
int auxiliary = 0, status = 1;
//Calculate number digit frequency
frequency(number, result);
for (int i = 0; i < 10 && status == 1; i++)
{
//Check that the resultant array
//[i] location frequency is greater than zero or not
if (result[i] != 0)
{
if (auxiliary == 0)
{
//get first number frequency
auxiliary = result[i];
}
else if (auxiliary != result[i])
{
//When no two numbers frequency are same in given number
//here, break the loop execution
status = 0;
}
}
}
if (status == 0)
{
Console.Write("" + number + " Is not a stable number\n");
}
else
{
Console.Write("" + number + " Is an stable number\n");
}
Console.Write("\n");
}
public static void Main(String[] args)
{
MyNumber obj = new MyNumber();
//Test Case
obj.stable_no(123);
obj.stable_no(1111);
obj.stable_no(89198);
obj.stable_no(112233);
obj.stable_no(10101);
}
}
Output
123 Is an stable number
1111 Is an stable number
89198 Is not a stable number
112233 Is an stable number
10101 Is not a stable number
/*
Java Program
Check if given number is stable or unstable
*/
public class MyNumber
{
//Calculate the frequency of digits in number
public void frequency(int number, int[] result)
{
int temp = 0;
while (number != 0)
{
temp = number % 10;
if (temp < 0)
{
//When get negative number
temp = -temp;
}
//modify the value of temp index element in result array
result[temp]++;
number /= 10;
}
}
public void stable_no(int number)
{
//resultant array which is store the frequency of number digits
int[] result = new int[10];
for (int i = 0; i < 10; ++i)
{
//Set the initial value of each element to zero
result[i] = 0;
}
int auxiliary = 0, status = 1;
//Calculate number digit frequency
frequency(number, result);
for (int i = 0; i < 10 && status == 1; ++i)
{
//Check that the resultant array
//[i] location frequency is greater than zero or not
if (result[i] != 0)
{
if (auxiliary == 0)
{
//get first number frequency
auxiliary = result[i];
}
else if (auxiliary != result[i])
{
//When no two numbers frequency are same in given number
//here, break the loop execution
status = 0;
}
}
}
if (status == 0)
{
System.out.print("" + number + " Is not a stable number\n");
}
else
{
System.out.print("" + number + " Is an stable number\n");
}
System.out.print("\n");
}
public static void main(String[] args)
{
MyNumber obj = new MyNumber();
//Test Case
obj.stable_no(123);
obj.stable_no(1111);
obj.stable_no(89198);
obj.stable_no(112233);
obj.stable_no(10101);
}
}
Output
123 Is an stable number
1111 Is an stable number
89198 Is not a stable number
112233 Is an stable number
10101 Is not a stable number
<?php
/*
Php Program
Check if given number is stable or unstable
*/
class MyNumber
{
//Calculate the frequency of digits in number
public function frequency($number, & $result)
{
$temp = 0;
while ($number != 0)
{
$temp = $number % 10;
if ($temp < 0)
{
//When get negative number
$temp = -$temp;
}
//modify the value of temp index element in result array
$result[$temp]++;
$number = intval($number / 10);
}
}
public function stable_no($number)
{
//resultant array which is store the frequency of number digits
//Set the initial value of each element to zero
$result = array_fill(0, 10, 0);
$auxiliary = 0;
$status = 1;
//Calculate number digit frequency
$this->frequency($number, $result);
for ($i = 0; $i < 10 && $status == 1; ++$i)
{
//Check that the resultant array
//[i] location frequency is greater than zero or not
if ($result[$i] != 0)
{
if ($auxiliary == 0)
{
//get first number frequency
$auxiliary = $result[$i];
}
else if ($auxiliary != $result[$i])
{
//When no two numbers frequency are same in given number
//here, break the loop execution
$status = 0;
}
}
}
if ($status == 0)
{
echo("". $number ." Is not a stable number\n");
}
else
{
echo("". $number ." Is an stable number\n");
}
echo("\n");
}
}
function main()
{
$obj = new MyNumber();
//Test Case
$obj->stable_no(123);
$obj->stable_no(1111);
$obj->stable_no(89198);
$obj->stable_no(112233);
$obj->stable_no(10101);
}
main();
Output
123 Is an stable number
1111 Is an stable number
89198 Is not a stable number
112233 Is an stable number
10101 Is not a stable number
/*
Node Js Program
Check if given number is stable or unstable
*/
class MyNumber
{
//Calculate the frequency of digits in number
frequency(number, result)
{
var temp = 0;
while (number != 0)
{
temp = number % 10;
if (temp < 0)
{
//When get negative number
temp = -temp;
}
//modify the value of temp index element in result array
result[temp]++;
number = parseInt(number / 10);
}
}
stable_no(number)
{
//resultant array which is store the frequency of number digits
//Set the initial value of each element to zero
var result = Array(10).fill(0);
var auxiliary = 0;
var status = 1;
//Calculate number digit frequency
this.frequency(number, result);
for (var i = 0; i < 10 && status == 1; ++i)
{
//Check that the resultant array
//[i] location frequency is greater than zero or not
if (result[i] != 0)
{
if (auxiliary == 0)
{
//get first number frequency
auxiliary = result[i];
}
else
if (auxiliary != result[i])
{
//When no two numbers frequency are same in given number
//here, break the loop execution
status = 0;
}
}
}
if (status == 0)
{
process.stdout.write("" + number + " Is not a stable number\n");
}
else
{
process.stdout.write("" + number + " Is an stable number\n");
}
process.stdout.write("\n");
}
}
function main(args)
{
var obj = new MyNumber();
//Test Case
obj.stable_no(123);
obj.stable_no(1111);
obj.stable_no(89198);
obj.stable_no(112233);
obj.stable_no(10101);
}
main();
Output
123 Is an stable number
1111 Is an stable number
89198 Is not a stable number
112233 Is an stable number
10101 Is not a stable number
# Python 3 Program
# Check if given number is stable or unstable
class MyNumber:
# Calculate the frequency of digits in number
def frequency(self, number, result):
temp = 0
while (number != 0):
temp = number % 10
if (temp < 0):
# When get negative number
temp = -temp
# modify the value of temp index element in result array
result[temp] += 1
number = int(number / 10)
def stable_no(self, number):
# Set the initial value of each element to zero
result = [0] * 10
i = 0
auxiliary = 0
status = 1
# Calculate number digit frequency
self.frequency(number, result)
while (i < 10 and status == 1):
# Check that the resultant array
# [i] location frequency is greater than zero or not
if (result[i] != 0):
if (auxiliary == 0):
# get first number frequency
auxiliary = result[i]
elif (auxiliary != result[i]):
# When no two numbers frequency are same in given number
# here, break the loop execution
status = 0
i += 1
if (status == 0):
print("", number ," Is not a stable number\n", end = "")
else:
print("", number ," Is an stable number\n", end = "")
print("\n", end = "")
def main():
obj = MyNumber()
# Test Case
obj.stable_no(123)
obj.stable_no(1111)
obj.stable_no(89198)
obj.stable_no(112233)
obj.stable_no(10101)
if __name__ == "__main__": main()
Output
123 Is an stable number
1111 Is an stable number
89198 Is not a stable number
112233 Is an stable number
10101 Is not a stable number
# Ruby Program
# Check if given number is stable or unstable
class MyNumber
# Calculate the frequency of digits in number
def frequency(number, result)
temp = 0
while (number != 0)
temp = number % 10
if (temp < 0)
# When get negative number
temp = -temp
end
# modify the value of temp index element in result array
result[temp] += 1
number /= 10
end
end
def stable_no(number)
# Set the initial value of each element to zero
result = Array.new(10) {0}
i = 0
auxiliary = 0
status = 1
# Calculate number digit frequency
self.frequency(number, result)
while (i < 10 && status == 1)
# Check that the resultant array
# [i] location frequency is greater than zero or not
if (result[i] != 0)
if (auxiliary == 0)
# get first number frequency
auxiliary = result[i]
elsif (auxiliary != result[i])
# When no two numbers frequency are same in given number
# here, break the loop execution
status = 0
end
end
i += 1
end
if (status == 0)
print("", number ," Is not a stable number\n")
else
print("", number ," Is an stable number\n")
end
print("\n")
end
end
def main()
obj = MyNumber.new()
# Test Case
obj.stable_no(123)
obj.stable_no(1111)
obj.stable_no(89198)
obj.stable_no(112233)
obj.stable_no(10101)
end
main()
Output
123 Is an stable number
1111 Is an stable number
89198 Is not a stable number
112233 Is an stable number
10101 Is not a stable number
/*
Scala Program
Check if given number is stable or unstable
*/
class MyNumber
{
//Calculate the frequency of digits in number
def frequency(num : Int, result: Array[Int]): Unit = {
var temp: Int = 0;
var number: Int = num;
while (number != 0)
{
temp = number % 10;
if (temp < 0)
{
//When get negative number
temp = -temp;
}
//modify the value of temp index element in result array
result(temp) += 1;
number = (number / 10).toInt;
}
}
def stable_no(number: Int): Unit = {
//Set the initial value of each element to zero
var result: Array[Int] = Array.fill[Int](10)(0);
var i: Int = 0;
var auxiliary: Int = 0;
var status: Int = 1;
//Calculate number digit frequency
frequency(number, result);
while (i < 10 && status == 1)
{
//Check that the resultant array
//[i] location frequency is greater than zero or not
if (result(i) != 0)
{
if (auxiliary == 0)
{
//get first number frequency
auxiliary = result(i);
}
else if (auxiliary != result(i))
{
//When no two numbers frequency are same in given number
//here, break the loop execution
status = 0;
}
}
i += 1;
}
if (status == 0)
{
print("" + number + " Is not a stable number\n");
}
else
{
print("" + number + " Is an stable number\n");
}
print("\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: MyNumber = new MyNumber();
//Test Case
obj.stable_no(123);
obj.stable_no(1111);
obj.stable_no(89198);
obj.stable_no(112233);
obj.stable_no(10101);
}
}
Output
123 Is an stable number
1111 Is an stable number
89198 Is not a stable number
112233 Is an stable number
10101 Is not a stable number
/*
Swift Program
Check if given number is stable or unstable
*/
class MyNumber
{
//Calculate the frequency of digits in number
func frequency(_ num: Int, _ result: inout [Int])
{
var temp: Int = 0;
var number: Int = num;
while (number != 0)
{
temp = number % 10;
if (temp < 0)
{
//When get negative number
temp = -temp;
}
//modify the value of temp index element in result array
result[temp] += 1;
number /= 10;
}
}
func stable_no(_ number: Int)
{
//Set the initial value of each element to zero
var result: [Int] = Array(repeating: 0, count: 10);
var i: Int = 0;
var auxiliary: Int = 0;
var status: Int = 1;
//Calculate number digit frequency
self.frequency(number, &result);
while (i < 10 && status == 1)
{
//Check that the resultant array
//[i] location frequency is greater than zero or not
if (result[i] != 0)
{
if (auxiliary == 0)
{
//get first number frequency
auxiliary = result[i];
}
else
if (auxiliary != result[i])
{
//When no two numbers frequency are same in given number
//here, break the loop execution
status = 0;
}
}
i += 1;
}
if (status == 0)
{
print("", number ," Is not a stable number\n", terminator: "");
}
else
{
print("", number ," Is an stable number\n", terminator: "");
}
print("\n", terminator: "");
}
}
func main()
{
let obj: MyNumber = MyNumber();
//Test Case
obj.stable_no(123);
obj.stable_no(1111);
obj.stable_no(89198);
obj.stable_no(112233);
obj.stable_no(10101);
}
main();
Output
123 Is an stable number
1111 Is an stable number
89198 Is not a stable number
112233 Is an stable number
10101 Is not a stable number
Resultant Output Explanation
The code is tested with five different test cases:
-
stable_no(123)
: The number 123 is stable because all its digits (1, 2, and 3) appear once. So, the output is "123 Is an stable number." -
stable_no(1111)
: The number 1111 is stable because all its digits (1) appear four times. So, the output is "1111 Is an stable number." -
stable_no(89198)
: The number 89198 is unstable because the digit '8' appears twice, '9' appears once, and '1' appears once. So, the output is "89198 Is not a stable number." -
stable_no(112233)
: The number 112233 is stable because all its digits (1, 2, and 3) appear twice. So, the output is "112233 Is an stable number." -
stable_no(10101)
: The number 10101 is unstable because the digit '0' appears twice, '1' appears three times, and '2' appears once. So, the output is "10101 Is not a stable number."
Time Complexity of the Code
The time complexity of the code is O(d), where 'd' is the number of digits in the input number. This is because the
frequency()
function iterates through each digit once, and the stable_no()
function also
iterates through the result
array once, which contains at most 10 elements (the number of digits).
Therefore, the overall time complexity is linear in terms of the number of digits in the input 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