![]() |
VOOZH | about |
An ugly number is a positive integer whose only prime factors are 2, 3, and 5, with 1 included by convention. The sequence begins as 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, and so on. Given an integer n, return the nth ugly number.
Examples:
Input: n = 5
Output: 5
Explanation: Ugly Numbers - 1, 2, 3, 4, 5, 6, 8, 9, 10, 12.
So, 5th Ugly Number is 5.Input: n = 10
Output: 12
Explanation: 12 is the 10th ugly number.
Table of Content
The idea is to loop over all positive integers starting from 1 until the count of ugly numbers in less than n. To check if number is ugly, divide the number by greatest divisible powers of 2, 3 and 5, if the number becomes 1 then it is an ugly number otherwise not.
12
Time Complexity: The time complexity is O(n^(1/3) * exp(n^(1/3))) since ugly numbers are sparse, and the nth ugly number is approximately exp(n^(1/3)). As a result, the loop runs up to exp(n^(1/3)) times, with each check taking O(n^(1/3)) time, leading to the overall complexity.
Space Complexity: O(1)
The idea is to use Set (set in C++, TreeSet in Java) to store the unique ugly numbers in sorted order. To do so, firstly insert the first ugly number i.e. 1 in the set and run a loop until the Nth ugly number is not found. At each iteration take out the smallest number x from the set, and insert three numbers (x * 2), (x * 3), and (x * 5), representing the next three ugly numbers. At last return the Nth number in the set.
12
The ugly number sequence consists of numbers that can only be divided by 2, 3, or 5, starting with 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, …. One way to view this sequence is by breaking it into three groups:
To generate ugly numbers efficiently, we maintain an array dp
[]of size n, initializing dp[0] = 1(the first ugly number). Additionally, we use:
- Three index pointers to track the next multiple of 2, 3, and 5.
- Three variables to store these next multiples.
Initially, the index pointers are set to 0, and the multiples are set to 2, 3, and 5. For each iteration from 1 to n, we:
- Select the smallest among the three multiples as the next ugly number.
- Store this number in dp
[].- Update the corresponding index and compute its next multiple using
arr[ind] * factor, where the factor is 2, 3, or 5.
Illustration: Finding the 5th Ugly Number
12
Note: This approach works best when we know the highest possible value of the ugly number. In our case, we are considering the maximum possible value to be 21474836647.
The idea is to use binary search to find the value such that the count of ugly numbers up to that value is equal to n. To do so, firstly create an array power[] of size 31, to store the power of 2 raised up to 30, this will be used to calculate the number of ugly numbers. Now perform binary search, taking low as 1, and high as 21474836647, which we are considering to be highest possible ugly number. At each iteration find the mid value.
Now, to calculate the count of ugly numbers up to mid value, use nested loops where outer loop increase in multiple of 5, and the inner loop increase in multiple of 3. For each iteration find the index of the first value greater than mid / i * j, (where i is multiple of 5, and j is multiple of 3) in array powers[]. Increment the count by the index.
At last check if count is greater than or equal to n, if so, update the answer to mid, and reduce high to mid - 1, otherwise increase low to mid + 1.
12
Time Complexity: O((log r)³), where r is the value of the Nth ugly number. Since we assume r = 21,474,836,647.
Auxiliary Space: O(1)