Intuition
The count of ugly numbers up to x only grows as x grows, so binary search for the smallest x whose count reaches n, counting divisibles with inclusion-exclusion.
Explanation
Why binary search on the answer
We do not need to list ugly numbers one by one. Define count(x) = how many integers in [1, x] are divisible by a, b, or c. As x grows by one, count(x) either stays the same or goes up by one, so it is non-decreasing. That monotonicity is the signal to binary search on the answer: find the smallest x with count(x) >= n. That x is itself ugly (the count only ticks up when we land on an ugly value), so it is exactly the n-th ugly number.
Counting divisibles with inclusion-exclusion
How many numbers in [1, x] are divisible by at least one of a, b, c? Multiples of a number x / a (integer division), and likewise for b and c. But adding those three sums double-counts numbers divisible by two of them, so we subtract the multiples of each pairwise least common multiple, then add back the multiples of the LCM of all three (subtracted one too many times):
count(x) = x/a + x/b + x/c - x/lcm(a,b) - x/lcm(a,c) - x/lcm(b,c) + x/lcm(a,b,c)
Every term is one division, so a count is O(1). (Use lcm(p, q) = p / gcd(p, q) * q to avoid overflow.)
Algorithm steps
- Precompute the three pairwise LCMs and the triple LCM.
- Set
lo = 1,hi = 2 * 10^9(the guaranteed upper bound). - While
lo < hi:mid = (lo + hi) / 2.- Compute
count(mid)with the formula above. - If
count(mid) >= n, the answer ismidor smaller: sethi = mid. - Otherwise set
lo = mid + 1.
- Return
lo.
Worked walk-through
n = 3, a = 2, b = 3, c = 5. The pairwise LCMs are 6, 10, 15 and the triple LCM is 30.
| Step | lo | hi | mid | count(mid) | count >= 3? | Action |
|---|---|---|---|---|---|---|
| 1 | 1 | 2000000000 | 1000000000 | big | yes | hi = mid |
| ... | (range keeps halving) | |||||
| late | 4 | 5 | 4 | 4/2+4/3+4/5 - 0 - 0 - 0 + 0 = 2+1+0 = 3 | yes | hi = 4 |
| last | 3 | 4 | 3 | 3/2+3/3+3/5 = 1+1+0 = 2 | no | lo = 4 |
lo meets hi at 4, and the ugly numbers are 2, 3, 4, 5, 6, ... so the 3rd is indeed 4.
Edge cases
- Two divisors equal (e.g.
a = b): the LCMs collapse naturally, so the formula still counts each value once. - n = 1: the answer is
min(a, b, c). - Overflow: pairwise LCMs can reach
10^18; computelcmasp / gcd(p, q) * qand use 64-bit integers.
Complexity
- Time: O(log(2 * 10^9)) ≈ 31 iterations, each an O(1) count — effectively constant.
- Space: O(1); only a handful of variables.
Implementation
class Solution {
public int nthUglyNumber(int n, int a, int b, int c) {
long la = a, lb = b, lc = c;
long ab = lcm(la, lb);
long ac = lcm(la, lc);
long bc = lcm(lb, lc);
long abc = lcm(ab, lc);
long lo = 1, hi = 2_000_000_000L;
while (lo < hi) {
long mid = lo + (hi - lo) / 2;
long count = mid / la + mid / lb + mid / lc
- mid / ab - mid / ac - mid / bc
+ mid / abc;
if (count >= n) {
hi = mid;
} else {
lo = mid + 1;
}
}
return (int) lo;
}
private long lcm(long x, long y) {
return x / gcd(x, y) * y;
}
private long gcd(long x, long y) {
while (y != 0) {
long t = y;
y = x % y;
x = t;
}
return x;
}
}