Skip to content

Commit b2f26f3

Browse files
chore: add LeetCode daily solution
1 parent 218fbc8 commit b2f26f3

File tree

5 files changed

+114
-0
lines changed

5 files changed

+114
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Make Sum Divisible by P (Medium)
2+
3+
**Problem ID:** 1590
4+
**Date:** 2025-11-30
5+
**Link:** https://leetcode.com/problems/make-sum-divisible-by-p/
6+
7+
## Approach
8+
9+
To solve the problem of finding the smallest subarray to remove such that the sum of the remaining elements is divisible by `p`, we can follow these steps:
10+
11+
### Main Idea:
12+
1. **Calculate Total Sum**: First, compute the total sum of the array `nums`. If this sum is already divisible by `p`, then no removal is needed, and we can return `0`.
13+
14+
2. **Determine Remainder**: If the total sum is not divisible by `p`, calculate the remainder when the total sum is divided by `p`. This remainder indicates how much we need to adjust the sum to make it divisible by `p`.
15+
16+
3. **Use Prefix Sums and HashMap**: To efficiently find the smallest subarray that can be removed:
17+
- Maintain a running prefix sum while iterating through the array.
18+
- Store the indices of the prefix sums modulo `p` in a HashMap. This allows us to quickly find the last occurrence of a specific remainder.
19+
- For each element, check if removing a subarray that leads to the desired adjustment (i.e., the prefix sum modulo `p` matching the required remainder) can yield a valid solution.
20+
21+
4. **Calculate Minimum Length**: For each valid prefix sum that can help achieve the desired sum, calculate the length of the subarray that would need to be removed. Keep track of the minimum length found.
22+
23+
### Data Structures:
24+
- An integer variable to store the total sum of the array.
25+
- A HashMap (or dictionary) to store the last seen index of each prefix sum modulo `p`.
26+
27+
### Complexity:
28+
- **Time Complexity**: O(n), where n is the length of the input array. We make a single pass through the array to compute the total sum and another pass to find the minimum removable subarray.
29+
- **Space Complexity**: O(p) in the worst case, due to storing the indices of prefix sums in the HashMap.
30+
31+
### Edge Cases:
32+
- If the total sum is less than `p`, check if removing any single element can achieve the desired condition.
33+
- If no valid subarray can be found, return `-1`.
34+
35+
By following this structured approach, we can efficiently determine the length of the smallest subarray to remove, ensuring that the solution is both optimal and clear.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public int minSubarray(int[] nums, int p) {
3+
long totalSum = 0;
4+
for (int num : nums) {
5+
totalSum += num;
6+
}
7+
8+
long remainder = totalSum % p;
9+
if (remainder == 0) return 0;
10+
11+
int n = nums.length;
12+
Map<Long, Integer> prefixMap = new HashMap<>();
13+
prefixMap.put(0L, -1);
14+
long currentSum = 0;
15+
int minLength = Integer.MAX_VALUE;
16+
17+
for (int i = 0; i < n; i++) {
18+
currentSum += nums[i];
19+
long target = (currentSum % p - remainder + p) % p;
20+
if (prefixMap.containsKey(target)) {
21+
minLength = Math.min(minLength, i - prefixMap.get(target));
22+
}
23+
prefixMap.put(currentSum % p, i);
24+
}
25+
26+
return minLength == Integer.MAX_VALUE ? -1 : minLength;
27+
}
28+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
var minSubarray = function(nums, p) {
2+
const totalSum = nums.reduce((a, b) => a + b, 0);
3+
const remainder = totalSum % p;
4+
5+
if (remainder === 0) return 0;
6+
7+
const n = nums.length;
8+
const map = new Map();
9+
map.set(0, -1);
10+
let currentSum = 0;
11+
let minLength = Infinity;
12+
13+
for (let i = 0; i < n; i++) {
14+
currentSum += nums[i];
15+
const mod = currentSum % p;
16+
17+
if (map.has((mod - remainder + p) % p)) {
18+
minLength = Math.min(minLength, i - map.get((mod - remainder + p) % p));
19+
}
20+
21+
map.set(mod, i);
22+
}
23+
24+
return minLength === Infinity ? -1 : minLength;
25+
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution:
2+
def minSubarray(self, nums: List[int], p: int) -> int:
3+
total_sum = sum(nums)
4+
remainder = total_sum % p
5+
6+
if remainder == 0:
7+
return 0
8+
9+
n = len(nums)
10+
min_length = float('inf')
11+
prefix_sum = 0
12+
prefix_map = {0: -1}
13+
14+
for i in range(n):
15+
prefix_sum += nums[i]
16+
current_remainder = prefix_sum % p
17+
18+
target_remainder = (current_remainder - remainder + p) % p
19+
20+
if target_remainder in prefix_map:
21+
min_length = min(min_length, i - prefix_map[target_remainder])
22+
23+
prefix_map[current_remainder] = i
24+
25+
return min_length if min_length != float('inf') else -1

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,3 +290,4 @@ Through completing the Blind 75 and NeetCode 150, you will have mastered:
290290
- 2025-11-23 — [Greatest Sum Divisible by Three](https://leetcode.com/problems/greatest-sum-divisible-by-three/) (Medium) → `Medium/2025-11-23-1262-Greatest-Sum-Divisible-by-Three`
291291
- 2025-11-24 — [Binary Prefix Divisible By 5](https://leetcode.com/problems/binary-prefix-divisible-by-5/) (Easy) → `Easy/2025-11-24-1018-Binary-Prefix-Divisible-By-5`
292292
- 2025-11-25 — [Smallest Integer Divisible by K](https://leetcode.com/problems/smallest-integer-divisible-by-k/) (Medium) → `Medium/2025-11-25-1015-Smallest-Integer-Divisible-by-K`
293+
- 2025-11-30 — [Make Sum Divisible by P](https://leetcode.com/problems/make-sum-divisible-by-p/) (Medium) → `Medium/2025-11-30-1590-Make-Sum-Divisible-by-P`

0 commit comments

Comments
 (0)