Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors. If there is no such integer in the array, return 0.
Example 1:
Input: nums = [21,4,7] Output: 32 Explanation: 21 has 4 divisors: 1, 3, 7, 21 4 has 3 divisors: 1, 2, 4 7 has 2 divisors: 1, 7 The answer is the sum of divisors of 21 only.
Example 2:
Input: nums = [21,21] Output: 64
Example 3:
Input: nums = [1,2,3,4,5] Output: 0
Constraints:
1 <= nums.length <= 1041 <= nums[i] <= 105Problem summary: Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors. If there is no such integer in the array, return 0.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Math
[21,4,7]
[21,21]
[1,2,3,4,5]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1390: Four Divisors
class Solution {
public int sumFourDivisors(int[] nums) {
int ans = 0;
for (int x : nums) {
ans += f(x);
}
return ans;
}
private int f(int x) {
int cnt = 2, s = x + 1;
for (int i = 2; i <= x / i; ++i) {
if (x % i == 0) {
++cnt;
s += i;
if (i * i != x) {
++cnt;
s += x / i;
}
}
}
return cnt == 4 ? s : 0;
}
}
// Accepted solution for LeetCode #1390: Four Divisors
func sumFourDivisors(nums []int) (ans int) {
f := func(x int) int {
cnt, s := 2, x+1
for i := 2; i <= x/i; i++ {
if x%i == 0 {
cnt++
s += i
if i*i != x {
cnt++
s += x / i
}
}
}
if cnt == 4 {
return s
}
return 0
}
for _, x := range nums {
ans += f(x)
}
return
}
# Accepted solution for LeetCode #1390: Four Divisors
class Solution:
def sumFourDivisors(self, nums: List[int]) -> int:
def f(x: int) -> int:
i = 2
cnt, s = 2, x + 1
while i <= x // i:
if x % i == 0:
cnt += 1
s += i
if i * i != x:
cnt += 1
s += x // i
i += 1
return s if cnt == 4 else 0
return sum(f(x) for x in nums)
// Accepted solution for LeetCode #1390: Four Divisors
impl Solution {
pub fn sum_four_divisors(nums: Vec<i32>) -> i32 {
let f = |x: i32| -> i32 {
let mut cnt = 2;
let mut s = x + 1;
let mut i = 2;
while i <= x / i {
if x % i == 0 {
cnt += 1;
s += i;
if i * i != x {
cnt += 1;
s += x / i;
}
}
i += 1;
}
if cnt == 4 { s } else { 0 }
};
let mut ans = 0;
for x in nums {
ans += f(x);
}
ans
}
}
// Accepted solution for LeetCode #1390: Four Divisors
function sumFourDivisors(nums: number[]): number {
const f = (x: number): number => {
let cnt = 2;
let s = x + 1;
for (let i = 2; i * i <= x; ++i) {
if (x % i === 0) {
++cnt;
s += i;
if (i * i !== x) {
++cnt;
s += Math.floor(x / i);
}
}
}
return cnt === 4 ? s : 0;
};
return nums.reduce((acc, x) => acc + f(x), 0);
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.