-
Notifications
You must be signed in to change notification settings - Fork 0
/
subarray-sums-divisible-by-k.cpp
78 lines (65 loc) · 1.8 KB
/
subarray-sums-divisible-by-k.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// https://leetcode.com/problems/subarray-sums-divisible-by-k/
//
//
// loop, but will TimeLimitExceed in some cases
class Solution {
public:
int subarraysDivByK(vector<int>& nums, int k) {
int sum;
int ans = 0;
int len = nums.size();
// deal with special case
if (len == 1) {
if (nums[0] % k == 0) return 1;
}
for (int i = 0; i < len - 1; i++) {
sum = nums[i];
if (sum % k == 0) ans++;
for (int j = i+1; j < len; j++) {
sum += nums[j];
if (sum % k == 0) ans++;
}
}
// deal with the last element
if (nums[len-1] % k == 0) ans++;
return ans;
}
};
// prefix sum
class Solution {
public:
int subarraysDivByK(vector<int>& nums, int k) {
map<int,int> mp; // remainder, cnt
mp[0] = 1; // assume the first element can be divided by k
int sum = 0;
int ans = 0;
int remainder = 0;
for (auto i : nums) {
sum += i;
remainder = (sum % k + k) % k; // for sum < 0
// if array A and B have the same sum remainder of K, then A-B can be divided by K
ans += mp[remainder];
mp[remainder]++;
}
return ans;
}
};
// replace map with index to improve the performance
class Solution {
public:
int subarraysDivByK(vector<int>& nums, int k) {
vector<int> idx(k,0); // idx: remainder, value: cnt
idx[0] = 1; // assume the first element can be divided by k
int sum = 0;
int ans = 0;
int remainder = 0;
for (auto i : nums) {
sum += i;
remainder = (sum % k + k) % k; // for sum < 0
// if array A and B have the same sum remainder of K, then A-B can be divided by K
ans += idx[remainder];
idx[remainder]++;
}
return ans;
}
};