A positive integer is called a Special Number if all the digits in its decimal representation lie between 1 to 5 (both inclusive). For example : 245, 312, etc. are some special numbers, whereas 340, 17, 0, etc. are some non-special numbers.
Given an integer 'N' . Your task is to find how many special numbers lie between 1 to N.
Input Format :
The first line of the input contains an integer, 'T,’ denoting the number of test cases.
The first and only line of each test case contains the integer 'N'.
Output Format :
For each test case, print an integer denoting the total count of special numbers between 1 to N.
Print the output of each test case in a new line.
Note :
You do not need to print anything. It has already been taken care of. Just implement the given function.
Constraints :
1 <= T <= 10^4
1 <= N <= 10^9
Time Limit: 1 sec
2
7
13
5
8
For the first test case :
All the special numbers that are smaller than 7 are [ 1, 2, 3, 4, 5 ]. Hence, the answer is 5 in this case.
For the second test case :
All the special numbers that are smaller than 13 are [ 1, 2, 3, 4, 5, 11, 12, 13 ]. Hence, the answer is 8 in this case.
2
2
20
2
10
Iterate through all numbers from 1 to N, and for every number, check whether it is a special number.
The idea is to iterate through all the numbers that are smaller than or equal to N one by one and find the total count of special numbers. To check whether a particular number is a special number, we can check the number digit by digit to determine whether all the digits lie between 1 to 5. If yes, then the number is a special number. Otherwise, the number is not a special number.
Steps:
As we are iterating through each value from 1 to N, and it takes O(log10(N)) time to check whether a particular number is a special number. Hence, the overall Time Complexity is O(N*log10(N)).
We are using only constant extra space. Hence, the overall Space Complexity is O(1).