


Let ‘N’ be: 10
The 10th ugly number is: 12
The first line of the input contains a single integer 'T', representing the number of test cases.
The first line of each test case contains a single integer ‘N’.
For each test case, print a single integer representing the ‘Nth’ ugly number.
Print the output of each test case in a separate line.
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10^4
Time Limit: 1 sec
The basic idea is to find the count of ugly numbers present less than a given number ‘N’. We use binary search to find the count. We initialize a range in which the result will be present and then use binary search to find the count of ugly numbers smaller than a number. We find it by finding the count of ugly numbers which are multiples of 2, 3, and 5, multiples of both 2 and 3, 3 and 5, and 2 and 5, and multiple of 2, 3, and 5.
HELPER(‘MID’)
HELP1(‘MID’, ‘VAL’) (where ‘VAl’ is multiple)
HELP2(‘MID’, ‘VAL1’, ‘VAL2’) (where ‘VAl1’ and ‘VAL2’ are multiples)
HELP3(‘MID’, ‘VAL1’, ‘VAL2’, ‘VAL3’) (where ‘VAL1’, ‘VAL2’, and ‘VAL3’ are multiples)
The basic idea is to store the multiples of the 2, 3, and 5 to find the ‘Nth’ ugly number. We create an array (say, ‘DP’) and find all the ugly numbers until ‘N’. We find the multiples of 2, 3, and 5 choose the minimum as ‘ith’ ugly number where 1 <= i <= N.
For example:
Initially, 1st ugly number is: 1
Potential next ugly numbers can be 1 * 2, 1 * 3, 1 * 5
We choose a minimum out of these for the next ugly number, i.e., 2.
To find the next ugly number, we take a minimum of 2 * 2, 1 * 3, 1 * 5, which will be 3.
Similarly, we find the ‘Nth’ ugly number.
Here is the algorithm :