-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContinuous Subarray Sum.cpp
More file actions
39 lines (30 loc) · 975 Bytes
/
Copy pathContinuous Subarray Sum.cpp
File metadata and controls
39 lines (30 loc) · 975 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
#include <vector>
class Solution {
public:
bool checkSubarraySum(std::vector<int>& numbers, int divisor) {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int currentSum = numbers[0];
int length = numbers.size();
int index, tempSum;
for (int i = 1; i < length; i++) {
if (numbers[i] == numbers[i - 1] && numbers[i] == 0) {
return true;
}
currentSum += numbers[i];
if (currentSum % divisor == 0) {
return true;
}
index = 0;
tempSum = currentSum;
while ((i - index) > 1 && tempSum >= divisor) {
tempSum -= numbers[index++];
if (tempSum % divisor == 0) {
return true;
}
}
}
return false;
}
};