You are given a non-negative integer ‘N’. Your task is to find the largest number that is less than or equal to ‘N’ with monotone increasing digits.
Note:
An integer has a monotone increasing digits if and only if each pair of adjacent digits x and y satisfy x <= y.
For example:
Given ‘N’ = 987
The answer is 899 because that is the greatest monotone increasing number possible that is less than ‘N’.
Input format:
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 single integer ‘N’.
Output format:
For each test case, print a single line containing a single integer denoting the maximum monotone number less than or equal to the given number.
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 <= 10
1 <= N <= 10 ^ 9
Where ‘T’ is the total number of test cases, and 'N’ is the number given to us.
Time limit: 1 sec.
2
789
987
789
899
For the first test case:
The answer is 789 because that is the greatest monotone increasing number possible that is equal to ‘N’.
For the second test case:
The answer is 899 because that is the greatest monotone increasing number possible.
2
10
11
9
11
Can we try all possible combinations?
The main idea is to try all possible combinations of numbers that are less than or equal to the given number ‘N’ and pick the greatest possible number.
O(log(N) * log(N)), where ‘N’ is the number given.
Since we are trying all possible digits and there at most ‘log(N)’ digits and each comparison take log(N) time, therefore O(log(N) * log(N)).
O(log (N)), where 'N' is the number given.
Since we are using a string to store the digits of the numbers.