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
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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code