Alice is very fond of digits. While she was playing with digits from 0 to 9, she noticed that when digits 0, 1, 6, 8, 9 are rotated 180 degrees, they become 0, 1, 9, 8, 6, respectively, and when digits 2, 3, 4, 5, and 7 are rotated 180 degrees, they become invalid.
After noticing such a property of digits, she started considering some numbers as strange numbers. According to her, a strange number is a number that, when rotated 180 degrees in a clockwise fashion, becomes a different number with each digit valid. As she is busy playing with digits, she gave you an integer ‘N’ and asked you to find the number of strange numbers from 1 to ‘N’ inclusive.
Note :
The rotated number can be greater(or smaller) than the original number.
Input Format :
The first line of the input contains an integer ‘T’ representing the number of test cases.
The first line of each test case contains a single integer ‘N’ denoting the integer given by Alice.
Output Format :
For each test case, print the number of strange numbers between 1 and ‘N’ inclusive.
The output of each test case will be printed in a separate line.
Note :
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints :
1 <= T <= 5
1 <= N <= 10 ^ 5
Time Limit: 1sec
2
5
10
0
3
Test case 1:
Numbers from 1 to 5 are 1, 2, 3, 4, 5.
1, when rotated 180 degrees, remains the same.
2, 3, 4, 5, when rotated 180 degrees, becomes invalid.
Therefore there are 0 strange numbers from 1 to 5.
Test case 2:
Strange numbers from 1 to 10 inclusive are 6, 9, and 10.
6, when rotated 180 degrees, becomes 9.
9, when rotated 180 degrees, becomes 6.
10, when rotated 180 degrees, becomes 01, which is the same as 1.
1
16
4
Test case 1:
Strange numbers from 1 to 16 inclusive are 6, 9, 10, and 16.
6, when rotated 180 degrees, becomes 9.
9, when rotated 180 degrees, becomes 6.
10, when rotated 180 degrees, becomes 01, which is the same as 1.
16, when rotated 180 degrees, becomes 91.
What are the possible digits in a strange number?. Try all numbers from 1 to ‘N’ with 0, 1, 6, 8, 9 as their digits.
The idea here is to perform a breadth-first search over all the numbers from 1 to ‘N’ inclusive with 0, 1, 6, 8, and 9 as their digits. Initially, we will insert all the numbers between 1 to ‘N’ inclusive having a single - digit with 0 or 1 or 6 or 8 or 9 as their digit into a queue. We will gradually increase the length(i.e., number of digits) of the numbers present in the queue and insert it into the queue if the number lies between 1 to ‘N’ inclusive. Refer to the below diagram for a clear understanding.
For all the numbers present in the queue, we will check if the current number is a strange number.
Algorithm:
Description of ‘isStrangeNumber’ function
This function is used to check whether the current number is a strange number or not.
This function will take one parameter:
bool isStrangeNumber(currNumber):
O(log(N) * 5 ^ K), where ‘N’ is the number given in the problem and ‘K’ is the number of digits in ‘N’.
In the worst case, we can have at most ‘K’ digits in any strange number, and for every digit, we have 5 choices, i.e., 0, 1, 6, 8, and 9. Therefore, the number of possible different strange numbers can be (5 ^ K), i.e.,
(5 choices for 1st digit) * (5 choices for 2nd digit) * . . . * (5 choices for Kth digit)
And for each such possible number, the ‘isStrangeNumber’ function is called, which takes O(log(N)) time.
So, the total time complexity will be O(log(N) * 5 ^ K)
O(5 ^ K), where ‘K’ is the number of digits in the given integer ‘N’.
In the worst case, we have to store all (5 ^ K) enumerated numbers in the queue. This case happens during the last level traversal of our BFS algorithm.
So, the total space complexity will be O(5 ^ K)