-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
count-array-pairs-divisible-by-k.cpp
44 lines (42 loc) · 1.22 KB
/
count-array-pairs-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
// Time: O(nlogk + sqrt(k)^2) = O(nlogk + k)
// Space: O(sqrt(k)), number of factors of k is at most sqrt(k)
// math, number theory
class Solution {
public:
long long countPairs(vector<int>& nums, int k) {
unordered_map<int64_t, int64_t> cnt;
for (const auto& x : nums) {
++cnt[gcd(x, k)];
}
int64_t result = 0;
for (const auto& [x, c1] : cnt) {
for (const auto& [y, c2] : cnt) {
if (x > y || x * y % k) {
continue;
}
result += (x != y) ? c1 * c2 : c1 * (c1 - 1) / 2;
}
}
return result;
}
};
// Time: O(nlogk + n * sqrt(k))
// Space: O(sqrt(k)), number of factors of k is at most sqrt(k)
// math, number theory
class Solution2 {
public:
long long countPairs(vector<int>& nums, int k) {
int64_t result = 0;
unordered_map<int64_t, int64_t> gcds;
for (const auto& i : nums) {
const int gcd_i = gcd(i, k);
for (const auto& [gcd_j, cnt] : gcds) {
if (gcd_i * gcd_j % k == 0) {
result += cnt;
}
}
++gcds[gcd_i];
}
return result;
}
};