Given a positive integer ‘N’, you have to find the closest number to 'N' which is greater than ‘N’, and has at most one non-zero digit. In other words, you have to find a number ‘K’ such that:
1. 'K' should be strictly greater than 'N'.
2. 'K' should contain at most 1 non-zero digit.
3. Difference between 'N' and 'K' should be minimum.
For eg.
Consider the number 546. The closest number greater than 546 that contains at most 1 non-zero digit is 600.
The first line of input contains an integer ‘T’ denoting the number of test cases.
The first and the only line of each test case contains a positive integer ‘N’.
Output Format
For each test case, print a single line containing a single integer denoting the number 'K' as described above in a single line.
The output for each test case will be printed in a separate line.
Note
You don’t have to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 1000
1 <= N <= 10 ^ 18
Time limit: 1 second
2
13
100
20
200
Test case 1:
We can see that the numbers greater than 13, having at most one non-zero digit are 20, 30, 40, 50... so on. The answer is 20 because it is the closest to 13.
Test case 2:
Clearly, the first greater number than 100 containing at most one non-zero digit is 200.
2
999
30001
1000
40000
Try to observe the mathematical pattern.
If we see the pattern, we can see that we just have to increment the first digit by one and replace the rest of the digits by zero.
For example:
Algorithm:
O(log10N), where ‘N’ is the given number.
We are just running a loop ‘count’-1 times where ‘count’ is the number of digits. Hence the time complexity is O(‘count’) or O(log10N).
O(1).
Since we are not using any extra space. We are just taking a variable which is constant space.