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, 2020Input: 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:
CPP
// C++ implementation to find // Autobiographical numbers with length N #include <bits/stdc++.h> using namespace std; // Function to return if the // number is autobiographical or not bool isAutoBio(int num) { string autoStr; int index, number, i, j, cnt; // Converting the integer // number to string autoStr = to_string(num); for (int i = 0; i < autoStr.size(); i++) { // Extracting each character // from each index one by one // and converting into an integer index = autoStr.at(i) - '0'; // Initialise count as 0 cnt = 0; for (j = 0; j < autoStr.size(); j++) { number = autoStr.at(j) - '0'; // Check if it is equal to the // index i if true then // increment the count if (number == i) // It is an // Autobiographical // number cnt++; } // Return false if the count and // the index number are not equal if (index != cnt) return false; } return true; } // Function to print autobiographical number // with given number of digits void findAutoBios(int n) { int high, low, i, flag = 0; // Left boundary of interval low = pow(10, n - 1); // Right boundary of interval high = pow(10, n) - 1; for (i = low; i <= high; i++) { if (isAutoBio(i)) { flag = 1; cout << i << ", "; } } // Flag = 0 implies that the number // is not an autobiographical no. if (!flag) cout << "There is no " << "Autobiographical number" << " with " << n << " digits\n"; } // Driver Code int main() { int N = 0; findAutoBios(N); N = 4; findAutoBios(N); return 0; } |
Java
// Java implementation to find // Autobiographical numbers with length N import java.util.*; import java.lang.Math; public class autobio { public static boolean isAutoBio(int num) { String autoStr; int index, number, i, j, cnt; // Converting the integer // number to string autoStr = Integer.toString(num); for (i = 0; i < autoStr.length(); i++) { // Extracting each character // from each index one by one // and converting into an integer index = Integer.parseInt(autoStr.charAt(i) + ""); // initialize count as 0 cnt = 0; for (j = 0; j < autoStr.length(); j++) { number = Integer.parseInt(autoStr.charAt(j) + ""); // Check if it is equal to the // index i if true then // increment the count if (number == i) // It is an // Autobiographical // number cnt++; } // Return false if the count and // the index number are not equal if (cnt != index) return false; } return true; } // Function to print autobiographical number // with given number of digits public static void findAutoBios(double n) { // both the boundaries are taken double, so as // to satisfy Math.pow() function's signature double high, low; int i, flag = 0; // Left boundary of interval low = Math.pow(10.0, n - 1); // Right boundary of interval 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 + ", "); } // Flag = 0 implies that the number // is not an autobiographical no. if (flag == 0) System.out.println("There is no Autobiographical Number" + "with " + (int)n + " digits"); } // Driver Code public static void main(String[] args) { double N = 0; findAutoBios(N); N = 4; findAutoBios(N); } } |
Python3
# Python implementation to find # Autobiographical numbers with length N from math import pow # Function to return if the # number is autobiographical or not def isAutoBio(num): # Converting the integer # number to string autoStr = str(num) for i in range(0, len(autoStr)): # Extracting each character # from each index one by one # and converting into an integer index = int(autoStr[i]) # Initialize count as 0 cnt = 0 for j in range(0, len(autoStr)): number = int(autoStr[j]) # Check if it is equal to the # index i if true then # increment the count if number == i: # It is an # Autobiographical # number cnt += 1 # Return false if the count and # the index number are not equal if cnt != index: return False return True # Function to print autobiographical number # with given number of digits def findAutoBios(n): # Left boundary of interval low = int(pow(10, n-1)) # Right boundary of interval high = int(pow(10, n) - 1) flag = 0 for i in range(low, high + 1): if isAutoBio(i): flag = 1 print(i, end =', ') # Flag = 0 implies that the number # is not an autobiographical no. if flag == 0: print("There is no Autobiographical Number with "+ str(n) + " digits") # Driver Code if __name__ == "__main__": N = 0 findAutoBios(N) N = 4 findAutoBios(N) |
There is no Autobiographical number with 0 digits 1210, 2020
Recommended Posts:
- Find all the prime numbers of given number of digits
- Print Nth Stepping or Autobiographical number
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Find smallest number with given number of digits and sum of digits under given constraints
- Find the average of k digits from the beginning and l digits from the end of the given number
- Find smallest number with given number of digits and sum of digits
- Find the Largest number with given number of digits and sum of digits
- Find the numbers from 1 to N that contains exactly k non-zero digits
- Find all palindrome numbers of given digits
- Find last k digits in product of an array numbers
- Find the maximum sum of digits of the product of two numbers
- Number of digits in the product of two numbers
- Check if two numbers have same number of digits
- Find maximum product of digits among numbers less than or equal to N
- Find the count of numbers that can be formed using digits 3, 4 only and having length at max N.
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 : saikiranjagini

