Find all Autobiographical Numbers with given number of digits
Given N as the number of digits, the task is to find all the Autobiographical Numbers whose length is equal to N.
An autobiographical number is a number such that the first digit of it counts how many zeroes are there in it, the second digit counts how many ones are there and so on.
For example, 1210 has 1 zero, 2 ones, 1 two and 0 threes.
Examples:
Input: N = 4
Output: 1210, 2020
Input: N = 5
Output: 21200
Approach: Any number with N-digits lies in the range [10(n-1), 10n-1]. So, each number in this range is iterated and checked if it is an Autobiographical number or not.
- Convert the number to a string
- iterate through each digit and store it in a variable.
- Then run an inner loop, compare the iterator of the outer loop with each digit of the inner loop and if they are equal then increment the occurrence count of the digit.
- Then check for equality between occurrence count and the variable in which each digit is stored so that we can know if the current number is autobiographical or not.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isAutoBio( int num)
{
string autoStr;
int index, number, i, j, cnt;
autoStr = to_string(num);
for ( int i = 0;
i < autoStr.size();
i++) {
index = autoStr.at(i) - '0' ;
cnt = 0;
for (j = 0; j < autoStr.size(); j++) {
number = autoStr.at(j) - '0' ;
if (number == i)
cnt++;
}
if (index != cnt)
return false ;
}
return true ;
}
void findAutoBios( int n)
{
int high, low, i, flag = 0;
low = pow (10, n - 1);
high = pow (10, n) - 1;
for (i = low; i <= high; i++) {
if (isAutoBio(i)) {
flag = 1;
cout << i << ", " ;
}
}
if (!flag)
cout << "There is no "
<< "Autobiographical number"
<< " with " << n
<< " digits\n" ;
}
int main()
{
int N = 0;
findAutoBios(N);
N = 4;
findAutoBios(N);
return 0;
}
|
Java
import java.util.*;
import java.lang.Math;
public class autobio {
public static boolean isAutoBio( int num)
{
String autoStr;
int index, number, i, j, cnt;
autoStr = Integer.toString(num);
for (i = 0 ; i < autoStr.length(); i++) {
index = Integer.parseInt(autoStr.charAt(i) + "" );
cnt = 0 ;
for (j = 0 ; j < autoStr.length(); j++) {
number = Integer.parseInt(autoStr.charAt(j) + "" );
if (number == i)
cnt++;
}
if (cnt != index)
return false ;
}
return true ;
}
public static void findAutoBios( double n)
{
double high, low;
int i, flag = 0 ;
low = Math.pow( 10.0 , n - 1 );
high = Math.pow( 10.0 , n) - 1.0 ;
for (i = ( int )low; i <= ( int )high; i++)
if (isAutoBio(i)) {
flag = 1 ;
System.out.print(i + ", " );
}
if (flag == 0 )
System.out.println( "There is no Autobiographical Number"
+ "with " + ( int )n + " digits" );
}
public static void main(String[] args)
{
double N = 0 ;
findAutoBios(N);
N = 4 ;
findAutoBios(N);
}
}
|
Python3
from math import pow
def isAutoBio(num):
autoStr = str (num)
for i in range ( 0 , len (autoStr)):
index = int (autoStr[i])
cnt = 0
for j in range ( 0 , len (autoStr)):
number = int (autoStr[j])
if number = = i:
cnt + = 1
if cnt ! = index:
return False
return True
def findAutoBios(n):
low = int ( pow ( 10 , n - 1 ))
high = int ( pow ( 10 , n) - 1 )
flag = 0
for i in range (low, high + 1 ):
if isAutoBio(i):
flag = 1
print (i, end = ', ' )
if flag = = 0 :
print ( "There is no Autobiographical Number with " + str (n) + " digits" )
if __name__ = = "__main__" :
N = 0
findAutoBios(N)
N = 4
findAutoBios(N)
|
C#
using System;
class autobio {
public static bool isAutoBio( int num)
{
String autoStr;
int index, number, i, j, cnt;
autoStr = num.ToString();
for (i = 0; i < autoStr.Length; i++) {
index = Int32.Parse(autoStr[i] + "" );
cnt = 0;
for (j = 0; j < autoStr.Length; j++) {
number = Int32.Parse(autoStr[j] + "" );
if (number == i)
cnt++;
}
if (cnt != index)
return false ;
}
return true ;
}
public static void findAutoBios( double n)
{
double high, low;
int i, flag = 0;
low = Math.Pow(10.0, n - 1);
high = Math.Pow(10.0, n) - 1.0;
for (i = ( int )low; i <= ( int )high; i++)
if (isAutoBio(i)) {
flag = 1;
Console.Write(i + ", " );
}
if (flag == 0)
Console.WriteLine( "There is no Autobiographical Number"
+ "with " + ( int )n + " digits" );
}
public static void Main(String[] args)
{
double N = 0;
findAutoBios(N);
N = 4;
findAutoBios(N);
}
}
|
Javascript
<script>
function isAutoBio(num){
let autoStr = num.toString()
for (let i=0;i<autoStr.length;i++){
let index = parseInt(autoStr[i])
let cnt = 0
for (let j=0;j<autoStr.length;j++){
let number = parseInt(autoStr[j])
if (number == i){
cnt += 1
}
}
if (cnt != index){
return false
}
}
return true
}
function findAutoBios(n){
let low = parseInt(Math.pow(10, n-1))
let high = parseInt(Math.pow(10, n) - 1)
let flag = 0
for (let i=low;i<high+1;i++){
if (isAutoBio(i)){
flag = 1
document.write(i, ', ' )
}
}
if (flag == 0)
document.write( "There is no Autobiographical Number with " + n + " digits" , "</br>" )
}
let N = 0
findAutoBios(N)
N = 4
findAutoBios(N)
</script>
|
Output:
There is no Autobiographical number with 0 digits
1210, 2020
Time Complexity: O(10n – 10n-1)
Auxiliary Space: O(1)
Last Updated :
09 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...