
Consider ‘N’ = 7, then ‘X’ = 13, because sum of factorial of its digits will be 1! + 3! = 1 + 6 = 7, and it is the smallest such integer.
1. ‘X’ may be large, so return it as a string.
2. A ‘X’, for a positive integer ‘N’, always exists under given constraints.
3. It is guaranteed that the number of digits in ‘X’ will not exceed 10^5.
The first line of input contains an integer ‘T’ denoting the number of test cases.
The next ‘T’ lines represent the ‘T’ test cases.
The first and only line of each test case contains the given integer ‘N’.
For each test case, print a string that represents the smallest positive integer ‘X’ such that the sum of the factorial of its digit is equal to ‘N’.
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 50
1 <= N <= 10^9
Time limit: 1 sec
Note: This approach will give ‘Time Limit Exceeded’ if ‘X’ is a large integer.
Notice that for any digit ‘d’ > 1, d! = d*((d-1)!). This implies that if you do not select digit ‘d’,then you need digit ‘d-1’, exactly ‘d’ times to have a digit factorial sum equal to single-digit ‘d’. It suggests giving priority to larger digits first, in order to find the smallest positive integer ‘X’.
For example, if ‘N’ = 40321, ‘X’ will be ‘18’ as 1! + 8! = 40321. f instead of digit ‘8’ in ‘X’, we select a smaller digit ‘7’ to be included as a digit in integer ‘X’, then the value of ‘X’ such that it includes 7 as the maximum digit will be 177777777, which is very large in comparison to 18, so we drop 7 and choose 8 instead.
So, our approach can be performed in the following steps -: