Four Divisors - Problem

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.

Note: A number has exactly four divisors when it is either:

  • The cube of a prime number (p³)
  • The product of two distinct prime numbers (p × q)

Input & Output

Example 1 — Mixed Numbers
$ Input: nums = [21,4,7]
Output: 32
💡 Note: 21 has divisors [1,3,7,21] (4 divisors, sum=32). 4 has divisors [1,2,4] (3 divisors). 7 has divisors [1,7] (2 divisors). Only 21 has exactly 4 divisors.
Example 2 — Multiple Valid Numbers
$ Input: nums = [21,21]
Output: 64
💡 Note: Both instances of 21 have exactly 4 divisors [1,3,7,21] with sum 32. Total: 32 + 32 = 64.
Example 3 — No Valid Numbers
$ Input: nums = [1,2,3,4,5]
Output: 0
💡 Note: 1 has 1 divisor, 2 has 2 divisors, 3 has 2 divisors, 4 has 3 divisors, 5 has 2 divisors. None have exactly 4 divisors.

Constraints

  • 1 ≤ nums.length ≤ 104
  • 1 ≤ nums[i] ≤ 105

Visualization

Tap to expand
Four Divisors Problem OverviewFind numbers with exactly 4 divisors and sum all their divisors21474 divisors ✓3 divisors ✗2 divisors ✗21 = 3 × 7 (product of two primes)Divisors: 1, 3, 7, 2113721Sum: 1 + 3 + 7 + 21 = 32Output: 32
Understanding the Visualization
1
Input Array
Array of integers to check
2
Find 4-Divisor Numbers
Identify which have exactly 4 divisors
3
Sum Divisors
Add up all divisors of valid numbers
Key Takeaway
🎯 Key Insight: Only p³ and p×q (distinct primes) have exactly 4 divisors
Asked in
Google 12 Facebook 8 Amazon 6
12.5K Views
Medium Frequency
~25 min Avg. Time
428 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen