You are given a number ‘N’. Your task is to find the N-th number that contains only the digits 0 1, 2, 3, 4, and 5.
Note:The number need not contain all the digits.
For example
Consider ‘N’ = 4, the numbers containing only the given digits are [0, 1, 2, 3]. The 4th number is 3. Hence the answer is 4.
The first line of input contains the integer ‘T’ representing the number of test cases.
The first line of each test case contains a single integer ‘N’, representing the given integer.
Output Format:
For each test case, print a single integer representing the N-th number containing only the given digits.
Print a separate line for each test case.
1 <= T <= 10
1 <= N <= 10^9
Time Limit: 1 sec
Note:
You do not need to print anything. It has already been taken care of. Just implement the function.
2
4
7
3
10
For the first test case N = 4, the numbers containing only the given digits are [0, 1, 2, 3]. The 4th number is 3. Hence the answer is 4.
For the second test case, N = 7, the numbers containing only the given digits are [0, 1, 2, 3, 4, 5, 10]. The 7th number is 10. Hence the answer is 10.
2
10
8
13
11
Build the numbers one by one.
In this approach, we will store the first 6 digits in an array. Then we can see the next numbers are [10, 11, 12, 13, 14, 15] and then [20, 21, 22, 23, 25, 25]. We can get the pattern by calculating the next 6 numbers like 10 + i, and then the next 6 as 20 + i, and so on. Each time we add 6 numbers, so we have to do this step N / 6 times.
Therefore we are multiplying by the power of 10 and add numbers from 0 to 6.
Algorithm:
O(N), Where N is the given number.
We are inserting N numbers in the array, which will take O(N) time. Hence the final time complexity is O(N).
O(N), Where N is the given number.
We are maintaining an array to store the integers that will cost O(N). Hence the final space complexity is O(N).