-
Notifications
You must be signed in to change notification settings - Fork 12
/
solution.java
34 lines (27 loc) · 1.01 KB
/
solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import java.util.HashMap;
class Solution {
public int minSubarray(int[] nums, int p) {
long totalSum = 0;
for (int num : nums) {
totalSum += num;
}
// Find remainder when total sum is divided by p
int rem = (int) (totalSum % p);
if (rem == 0)
return 0; // If remainder is 0, no subarray needs to be removed
HashMap<Integer, Integer> prefixMod = new HashMap<>();
prefixMod.put(0, -1); // Initialize to handle full prefix
long prefixSum = 0;
int minLength = nums.length;
for (int i = 0; i < nums.length; ++i) {
prefixSum += nums[i];
int currentMod = (int) (prefixSum % p);
int targetMod = (currentMod - rem + p) % p;
if (prefixMod.containsKey(targetMod)) {
minLength = Math.min(minLength, i - prefixMod.get(targetMod));
}
prefixMod.put(currentMod, i);
}
return minLength == nums.length ? -1 : minLength;
}
}