-
Notifications
You must be signed in to change notification settings - Fork 0
/
1590.cpp
44 lines (34 loc) · 944 Bytes
/
1590.cpp
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
35
36
37
38
39
40
41
42
43
44
// MOHIT-IITP template
#include <bits/stdc++.h>
using namespace std
#define ll long long
#define pb push_back
#define MOD 1000000007
class Solution {
public:
int minSubarray(vector<int>& nums, int p) {
int n = nums.size();
int totalSum = 0;
for (int num : nums) {
totalSum = (totalSum + num) % p;
}
int target = totalSum % p;
if (target == 0) return 0;
unordered_map<int, int> modMap;
modMap[0] =
-1;
int currentSum = 0, minLen = n;
for (int i = 0; i < n; ++i) {
currentSum = (currentSum + nums[i]) % p;
int needed = (currentSum - target + p) % p;
if (modMap.find(needed) != modMap.end()) {
minLen = min(minLen, i - modMap[needed]);
}
modMap[currentSum] = i;
}
return minLen == n ? -1 : minLen;
}
};
int main() {
return 0;
}