Skip to content

Commit 962ede1

Browse files
chore: add LeetCode daily solution
1 parent 919ab15 commit 962ede1

5 files changed

Lines changed: 109 additions & 0 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public int sumFourDivisors(int[] nums) {
3+
int sum = 0;
4+
for (int num : nums) {
5+
int count = 0;
6+
int divisorSum = 0;
7+
for (int i = 1; i * i <= num; i++) {
8+
if (num % i == 0) {
9+
if (i * i == num) {
10+
count++;
11+
divisorSum += i;
12+
} else {
13+
count += 2;
14+
divisorSum += i + (num / i);
15+
}
16+
}
17+
if (count > 4) break;
18+
}
19+
if (count == 4) {
20+
sum += divisorSum;
21+
}
22+
}
23+
return sum;
24+
}
25+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
var sumFourDivisors = function(nums) {
2+
let totalSum = 0;
3+
4+
const hasFourDivisors = (n) => {
5+
let divisors = [];
6+
for (let i = 1; i * i <= n; i++) {
7+
if (n % i === 0) {
8+
divisors.push(i);
9+
if (i !== n / i) {
10+
divisors.push(n / i);
11+
}
12+
}
13+
if (divisors.length > 4) return null;
14+
}
15+
return divisors.length === 4 ? divisors.reduce((a, b) => a + b) : null;
16+
};
17+
18+
for (const num of nums) {
19+
const sum = hasFourDivisors(num);
20+
if (sum) {
21+
totalSum += sum;
22+
}
23+
}
24+
25+
return totalSum;
26+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def sumFourDivisors(self, nums: List[int]) -> int:
3+
def get_divisors_sum(n):
4+
divisors = []
5+
for i in range(1, int(n**0.5) + 1):
6+
if n % i == 0:
7+
divisors.append(i)
8+
if i != n // i:
9+
divisors.append(n // i)
10+
if len(divisors) > 4:
11+
return 0
12+
return sum(divisors) if len(divisors) == 4 else 0
13+
14+
total_sum = 0
15+
for num in nums:
16+
total_sum += get_divisors_sum(num)
17+
18+
return total_sum

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,3 +325,4 @@ Through completing the Blind 75 and NeetCode 150, you will have mastered:
325325
- 2026-01-01 — [Plus One](https://leetcode.com/problems/plus-one/) (Easy) → `Easy/2026-01-01-66-Plus-One`
326326
- 2026-01-02 — [N-Repeated Element in Size 2N Array](https://leetcode.com/problems/n-repeated-element-in-size-2n-array/) (Easy) → `Easy/2026-01-02-961-N-Repeated-Element-in-Size-2N-Array`
327327
- 2026-01-03 — [Number of Ways to Paint N × 3 Grid](https://leetcode.com/problems/number-of-ways-to-paint-n-3-grid/) (Hard) → `Hard/2026-01-03-1411-Number-of-Ways-to-Paint-N-3-Grid`
328+
- 2026-01-04 — [Four Divisors](https://leetcode.com/problems/four-divisors/) (Medium) → `Medium/2026-01-04-1390-Four-Divisors`

0 commit comments

Comments
 (0)