LeetCode Challenge Day 111 β 1390. Four Divisors
Nitin Ahirwal / January 4, 2026
Hey folks π
This is Day 111 of my LeetCode streak π
Today's problem is 1390. Four Divisors β a neat number theory problem where recognizing divisor patterns helps avoid brute force.
π Problem Statement
You are given:
- An integer array
nums
Goal:
For every number in the array that has exactly four divisors, return the sum of its divisors.
If no such number exists, return 0.
π‘ Intuition
Divisors always come in pairs.
For a number n, if i is a divisor, then n / i is also a divisor.
This means we only need to iterate up to βn.
The key idea:
As soon as a number has more than four divisors, it becomes irrelevant and we can stop checking it further.
This early stopping keeps the solution efficient even for larger numbers.
π Approach
- Initialize
totalSumto store the final answer. - For each number
ninnums:- Start with two guaranteed divisors:
1andn. - Iterate from
2toβn. - If
idividesn, add bothiandn / ito the divisor list. - If the divisor count exceeds
4, break early.
- Start with two guaranteed divisors:
- If the number ends up with exactly four divisors, add their sum to
totalSum. - Return
totalSum.
This approach balances clarity and performance without over-engineering.
β±οΈ Complexity Analysis
-
Time Complexity:
O(n Γ βm)
wherenis the length of the array andmis the maximum value innums. -
Space Complexity:
O(1)
Only a small list of at most four divisors is maintained.
π§βπ» Code (JavaScript)
/**
* @param {number[]} nums
* @return {number}
*/
var sumFourDivisors = function(nums) {
let totalSum = 0;
for (let n of nums) {
let divisors = [1, n];
for (let i = 2; i * i <= n; i++) {
if (n % i === 0) {
divisors.push(i);
if (i !== n / i) {
divisors.push(n / i);
}
if (divisors.length > 4) break;
}
}
if (divisors.length === 4) {
totalSum += divisors.reduce((a, b) => a + b, 0);
}
}
return totalSum;
};
π― Reflection
This problem reinforces an important lesson:
-
Not every problem needs complex math β early pruning goes a long way
-
Understanding divisor behavior can drastically reduce work
-
Clean loops + logical breaks = efficient solutions
That wraps up Day 111 of my LeetCode challenge π₯
Onward to Day 112 β consistency wins π
Happy Coding π¨βπ»