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.
Given an integer n, return true if n has exactly three positive divisors. Otherwise, return false.
An integer m is a divisor of n if there exists an integer k such that n = k * m.
Example 1:
Input: n = 2 Output: false Explantion: 2 has only two divisors: 1 and 2.
Example 2:
Input: n = 4 Output: true Explantion: 4 has three divisors: 1, 2, and 4.
Constraints:
1 <= n <= 104Problem summary: Given an integer n, return true if n has exactly three positive divisors. Otherwise, return false. An integer m is a divisor of n if there exists an integer k such that n = k * m.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math
2
4
find-greatest-common-divisor-of-array)smallest-even-multiple)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1952: Three Divisors
class Solution {
public boolean isThree(int n) {
int cnt = 0;
for (int i = 2; i < n; i++) {
if (n % i == 0) {
++cnt;
}
}
return cnt == 1;
}
}
// Accepted solution for LeetCode #1952: Three Divisors
func isThree(n int) bool {
cnt := 0
for i := 2; i < n; i++ {
if n%i == 0 {
cnt++
}
}
return cnt == 1
}
# Accepted solution for LeetCode #1952: Three Divisors
class Solution:
def isThree(self, n: int) -> bool:
return sum(n % i == 0 for i in range(2, n)) == 1
// Accepted solution for LeetCode #1952: Three Divisors
struct Solution;
impl Solution {
fn is_three(n: i32) -> bool {
let mut count = 2;
for i in 2..n {
if n % i == 0 {
count += 1;
}
}
count == 3
}
}
#[test]
fn test() {
let n = 2;
let res = false;
assert_eq!(Solution::is_three(n), res);
let n = 4;
let res = true;
assert_eq!(Solution::is_three(n), res);
}
// Accepted solution for LeetCode #1952: Three Divisors
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #1952: Three Divisors
// class Solution {
// public boolean isThree(int n) {
// int cnt = 0;
// for (int i = 2; i < n; i++) {
// if (n % i == 0) {
// ++cnt;
// }
// }
// return cnt == 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.