-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathContinuousSubarraySum.cpp
More file actions
40 lines (33 loc) · 898 Bytes
/
Copy pathContinuousSubarraySum.cpp
File metadata and controls
40 lines (33 loc) · 898 Bytes
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
class Solution {
public:
bool checkSubarraySum(vector<int>& nums, int k) {
int n = nums.size();
if(n <= 1){
return 0;
}
else if(k == 0){
for(int i = 1; i < n; i++){
if(nums[i] == 0 && nums[i-1] == 0){
return true;
}
}
return false;
}
vector<long long int> sum(n, 0);
sum[0] = nums[0];
for(int i = 1; i < n; i++){
sum[i] = sum[i-1] + nums[i];
}
for(int i = 1; i < n; i++){
if(sum[i]%k == 0){
return true;
}
for(int j = i-2; j >= 0; j--){
if((sum[i]-sum[j])%k == 0){
return true;
}
}
}
return false;
}
};