Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Build confidence with an intuition-first walkthrough focused on math fundamentals.
An ugly number is a positive integer which does not have a prime factor other than 2, 3, and 5.
Given an integer n, return true if n is an ugly number.
Example 1:
Input: n = 6 Output: true Explanation: 6 = 2 × 3
Example 2:
Input: n = 1 Output: true Explanation: 1 has no prime factors.
Example 3:
Input: n = 14 Output: false Explanation: 14 is not ugly since it includes the prime factor 7.
Constraints:
-231 <= n <= 231 - 1Problem summary: An ugly number is a positive integer which does not have a prime factor other than 2, 3, and 5. Given an integer n, return true if n is an ugly number.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math
6
1
14
happy-number)count-primes)ugly-number-ii)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #263: Ugly Number
class Solution {
public boolean isUgly(int n) {
if (n < 1) return false;
while (n % 2 == 0) {
n /= 2;
}
while (n % 3 == 0) {
n /= 3;
}
while (n % 5 == 0) {
n /= 5;
}
return n == 1;
}
}
// Accepted solution for LeetCode #263: Ugly Number
func isUgly(n int) bool {
if n < 1 {
return false
}
for _, x := range []int{2, 3, 5} {
for n%x == 0 {
n /= x
}
}
return n == 1
}
# Accepted solution for LeetCode #263: Ugly Number
class Solution:
def isUgly(self, n: int) -> bool:
if n < 1:
return False
for x in [2, 3, 5]:
while n % x == 0:
n //= x
return n == 1
// Accepted solution for LeetCode #263: Ugly Number
impl Solution {
pub fn is_ugly(n: i32) -> bool {
if n < 1 {
return false;
}
let mut n = n;
let ugly_primes = vec![2, 3, 5];
for prime in ugly_primes {
while n % prime == 0 {
n /= prime;
}
}
n == 1
}
}
// Accepted solution for LeetCode #263: Ugly Number
function isUgly(n: number): boolean {
if (n < 1) {
return false;
}
for (let prime of [2, 3, 5]) {
while (n % prime == 0) {
n /= prime;
}
}
return n == 1;
}
Use this to step through a reusable interview workflow for this problem.
Simulate the process step by step — multiply n times, check each number up to n, or iterate through all possibilities. Each step is O(1), but doing it n times gives O(n). No extra space needed since we just track running state.
Math problems often have a closed-form or O(log n) solution hidden behind an O(n) simulation. Modular arithmetic, fast exponentiation (repeated squaring), GCD (Euclidean algorithm), and number theory properties can dramatically reduce complexity.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.