Given an integer N, the task is to find all the self-descriptive numbers from 1 to N
A self-descriptive number is an integer n in given base b is b digits long in which each digit at position p (the most significant digit being at position 0 and the least significant at position b – 1) counts how many times a digit p is in n.
For example in base 10, 6210001000 is a self descriptive number as there are six 0s, two 1s, one 2 and one 6. Explanation : It is 10 digit number in base 10. It has 6 at the position 0 and there are six 0s in 6210001000. It has 2 at the position 1 and there are two 1s in 6210001000. It has 1 at the position 2 and there is one 2s in 6210001000. It has 0 at the position 3 and there are zero 3s in 6210001000. It has 0 at the position 4 and there are zero 4s in 6210001000. It has 0 at the position 5 and there are zero 5s in 6210001000. It has 1 at the position 6 and there is one 6 in 6210001000. It has 0 at the position 7 and there are zero 7s in 6210001000. It has 0 at the position 8 and there are zero 8s in 6210001000. It has 0 at the position 9 and there are zero 9s in 6210001000. [Source : Wikipedia]
Input: N = 10000 Output: 1210 2020 Explanation: From 1 to N only these two numbers are the self-descriptive numbers
Input: N = 10 Output: Explanation: There is no self descriptive number in range [1, 10]
Here is a program to print all self-descriptive numbers below 100000000. In the below program we have just ignored one fact about the self-descriptive number that it should have as many number of digits as much the base is given.
Description of Program : 1 . Firstly all the digits get extracted from the outer loop and are stored in a variable b in each iteration. 2 . Then in the inner loop there is a count on how many times number i (this i is ith index of outer loop) is present in the string. 3 . Finally that count is compared with the digit present at the ith index stored in variable b.
Please Login to comment...