
The first line of input contains an integer T representing the number of test cases.
The first line of each test case contains one integer N denoting the number.
For each test case, on a separate line, output one integer - Nth special number.
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 1000
1 <= N <= 10^6
Where ‘T’ is the number of test cases, ‘N’ the given number.
Time limit: 1 sec
The first six special numbers are 0, 1, 2, 3, 4 and 5. The next six special numbers are 10, 11, 12, 13, 14 and 15. The next six special numbers are 20, 21, 22, 23, 24, 25, and so on.
A pattern can be observed here for the chains of six-
Let’s say we have an ans array initially filled with numbers from 0 to 5, i.e the 1st six special numbers. The next chain of numbers in the series would be nothing but 10 * 1 + 0 = 10, 10 * 1 + 1, ……., 10 * 1 + 5. The next chain would be 10 * 2 + 0, 10 * 2 + 1, ………., 10 * 2 + 5.
Similarly, the i-th chain would be 10 * ans[i] + 0, 10 * ans[i] + 1, ………, 10 * ans[i] + 5.
The algorithm is as follows -
If we have a number with a base of 10, then each digit of the number will be from 0 to 9. Similarly, if each digit of a number is from 0 to X, then the number can be represented in base X+1. Since a special number has digits from 0 to 5, and the special number series starts from 0, we observe that specialNumber(N) is equal to the base 6 representation of N - 1.
Hence to find the Nth special number it is sufficient to find the base 6 representation of N - 1. This can be done with the help of the following recursive formula.
specialNumber(N) = (N) % 6) + (10 * specialNumber((N) / 6)).
For example, the base 6 representation of 15 is 23, by repeatedly dividing N by 6 and storing the remainders.
15 base 6 = (15 % 6) + 10 * Base 6 representation of (15 / 6).
= 3 + (10 * Base 6 representation of (2))
= 3 + (10 * 2)
= 23
Here, digit 2 corresponds to 10^1-th power, and digit 3 corresponds to 10^0-th power.
The algorithm is as follows -
Merge Two Sorted Arrays Without Extra Space
Merge Two Sorted Arrays Without Extra Space
Co-Prime
Inverted Left Half Pyramid
First Digit One
Special Digit Numbers