|
| 1 | +# Four Divisors (Medium) |
| 2 | + |
| 3 | +**Problem ID:** 1390 |
| 4 | +**Date:** 2026-01-04 |
| 5 | +**Link:** https://leetcode.com/problems/four-divisors/ |
| 6 | + |
| 7 | +## Approach |
| 8 | + |
| 9 | +To solve the "Four Divisors" problem, we need to identify integers in the given array that have exactly four divisors and compute the sum of those divisors. Here's a structured approach to tackle this problem: |
| 10 | + |
| 11 | +### Main Idea: |
| 12 | +1. **Understanding Divisors**: An integer has exactly four divisors if it can be expressed in one of two forms: |
| 13 | + - A product of two distinct prime numbers, \( p \times q \), where \( p \) and \( q \) are prime. The divisors in this case are \( 1, p, q, \) and \( p \times q \). |
| 14 | + - A cube of a prime number, \( p^3 \), where the divisors are \( 1, p, p^2, \) and \( p^3 \). |
| 15 | + |
| 16 | +2. **Efficient Prime Generation**: Since the maximum value for elements in `nums` is \( 10^5 \), we can use the Sieve of Eratosthenes to generate all prime numbers up to \( 10^5 \). This allows us to quickly check if a number is prime and to find its divisors. |
| 17 | + |
| 18 | +### Steps: |
| 19 | +1. **Generate Primes**: Use the Sieve of Eratosthenes to create a list of all primes up to \( 10^5 \) and store them in a set for O(1) lookups. |
| 20 | + |
| 21 | +2. **Iterate through the Array**: For each number in the `nums` array: |
| 22 | + - Check if it has exactly four divisors: |
| 23 | + - **Form 1**: Check if it can be expressed as \( p \times q \) where both \( p \) and \( q \) are distinct primes. If found, compute the sum of divisors \( 1 + p + q + (p \times q) \). |
| 24 | + - **Form 2**: Check if it is of the form \( p^3 \) for some prime \( p \). If so, compute the sum of divisors \( 1 + p + p^2 + p^3 \). |
| 25 | + |
| 26 | +3. **Accumulate Results**: Maintain a running total of the sums of divisors for all numbers that meet the criteria. |
| 27 | + |
| 28 | +4. **Return the Result**: After processing all numbers, return the accumulated sum. If no numbers had exactly four divisors, return 0. |
| 29 | + |
| 30 | +### Data Structures: |
| 31 | +- A list or array to hold the prime numbers generated by the sieve. |
| 32 | +- A set for quick prime checks. |
| 33 | +- A variable to accumulate the sum of divisors. |
| 34 | + |
| 35 | +### Complexity: |
| 36 | +- **Time Complexity**: The Sieve of Eratosthenes runs in \( O(n \log \log n) \) where \( n \) is \( 10^5 \). Checking each number in `nums` requires factorization that, in the worst case, can be handled in \( O(\sqrt{m}) \) where \( m \) is the number being checked, leading to an overall complexity of \( O(n \sqrt{m}) \) for the entire array. |
| 37 | +- **Space Complexity**: The space used for the sieve is \( O(n) \) for storing primes. |
| 38 | + |
| 39 | +This approach efficiently identifies numbers with exactly four divisors and calculates their divisor sums, adhering to the constraints provided in the problem statement. |
0 commit comments