You are given an integer, βMAXVALβ. Your task is to determine the total number of special numbers present in the range, 1 to βMAXVALβ.
Note:A special number is a number, which when rotated 180 degrees, resembles some other number in the same range. Every digit of a special number must be a valid digit.
The digits, 0,1,6,8,9, when rotated 180 degrees, become 0,1,9,8,6 respectively. While the digits, 2,3,4,5,7, when rotated do not become any valid digit.
For example:
β8196β, when rotated 180 degrees, will become β9618β. We can observe that all the digits of this number are valid. So, this is a special number.
The first line contains an integer βTβ, which denotes the number of test cases to be run. Then, the T test cases follow.
The first line of each test case contains a single integer, βMAXVALβ.
Output Format:
For each test case, print a single integer, denoting the total number of special numbers in the range, 1 to βMAXVALβ.
Output for 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.
1 <= T <= 10
1 <= MAXVAL < 10^4
Time Limit: 1sec
1
10
2
There are only two special numbers in the range from β1β to β10β. These two numbers are β6β and β9β.
We can not consider β1β and β8β because these numbers, when rotated, give the same number back so they can not be termed as special numbers. β10β can also not be considered as a special number because, after rotation, it becomes β01β. Although, the numerical value of β01β is also β1β, the representation of β01β is different from β1β.
Hence, there are only two special numbers in this range.
1
16
2
Check for all numbers in the range independently
The approach is to loop through all the numbers in the given range one by one and check whether this number is a special number or not. We can check if a number is special or not, by rotating it. This can be achieved by reversing the string representation of the number and thereafter, replacing every β6β by β9β and vice versa.
O(N * log N), where N is the value of βMAXVALβ.
We are checking all the numbers in the range 1 to N, one by one, and checking each digit of the number. Since a number can have log N digits.
So the overall complexity becomes O(N * logN).
O(1)
We are not using any extra space.