Javatpoint Logo
Javatpoint Logo

Self-Descriptive Numbers in Java

A number n is given. Our task is to find out the self-descriptive numbers that are present between 1 to n.

Self-Descriptive Numbers

A self-descriptive number m, is a number that contains b digits in the base b, where the most significant digit is positioned at 0 and the least significant digit positioned at b - 1 (left to right and zero indexing (similar to an array)). Also, every digit d is positioned at n, counting how many times the digit n is there in m. Observe the following examples.

Example 1:

m = 2020

Output: 2020 is a self-descriptive number.

Explanation: Since the number 2020 contains only 4 digits, therefore, base 4 is considered. In the number 2 is positioned at the 0th position, which means 0 occurs two times. 0 is positioned at 1st position, which means 1 is occurring zero times. Again, 2 is positioned at 2nd position, meaning 2 is occurring two times. After that, 0 is placed at the 3rd position, which means 3 occurs zero times. If we see the number, we find that

0 is occurring two times. 2 is occurring two times. Also, 1 and 3 occur zero times. Thus, all the statements written in the above paragraph are correct. Hence, 2020 is a self-descriptive number.

Example 2:

m = 6210001000

Output: 6210001000 is a self-descriptive number.

Explanation: Since the number 2020 contains only 10 digits, therefore, base 10 is considered. In the number 6 is positioned at 0th position, which means 0 occurs six times. 2 is positioned at 1st position, which means 1 is occurring two times. Again, 1 is positioned at 2nd position, meaning 2 is occurring one time. After that 0 is placed at the 3rd position, 4th position, 5th position, 7th position, 8th position, and 9th position, which means numbers 3, 4, 5, 7, 8, and 9 occur zero times. At 6th position, we see the number 1, which means 6 occurs one time. If we see the number, we find that

6 is occurring one time. 2 is occurring one time. 1 is occurring two times. 2 occurs one time, and 0 occurs six times. Also, 3, 4, 5, 7, 8, and 9 occur zero times. Thus, all the statements written in the above paragraph are correct. Hence, 6210001000 is a self-descriptive number.

Implementation: Using Nested Loop

Observe the following program for finding the self-descriptive number.

FileName: SelfDescriptive.java

Output:

In the range 1 to 100000, we have the following self-descriptive numbers.
1210
2020
21200

Explanation: In the outer loop, all of the digits are getting extracted and kept in the variable v in every iteration. Then inside the inner loop, a variable cnt is there to computing the number of times the number j (this j is the jth index of the outer loop) is occurring in the string. Eventually, the variable cnt is compared with the variable v. If the comparison does not match, a false value is returned.

Complexity Analysis: The time complexity of the program is O(n * len(n) * len(n)). The space complexity of the program is O(1).

If we do some preprocessing to store the frequency of the digits in an array, then we can reduce the complexity of the program. The illustration of the same is given in the following program.

FileName: SelfDescriptive1.java

Output:

In the range 1 to 100000, we have the following self-descriptive numbers.
1210
2020
21200

Complexity Analysis: The time complexity of the program is O(n * len(n)). The space complexity of the program is O(1). One may argue that we are using an auxiliary array. However, the auxiliary array is of the size 10 and is not varying in any scenario. Thus, we can say that the auxiliary array is of a fixed size making the space complexity O(1).







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA