Back to posts

LeetCode Challenge Day 111 β€” 1390. Four Divisors

Nitin Ahirwal / January 4, 2026

LeetCode ChallengeDay 111Number TheoryMathOptimizationJavaScriptMedium

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

  1. Initialize totalSum to store the final answer.
  2. For each number n in nums:
    • Start with two guaranteed divisors: 1 and n.
    • Iterate from 2 to √n.
    • If i divides n, add both i and n / i to the divisor list.
    • If the divisor count exceeds 4, break early.
  3. If the number ends up with exactly four divisors, add their sum to totalSum.
  4. Return totalSum.

This approach balances clarity and performance without over-engineering.


⏱️ Complexity Analysis

  • Time Complexity:
    O(n Γ— √m)
    where n is the length of the array and m is the maximum value in nums.

  • 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 πŸ‘¨β€πŸ’»