-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
count-equal-and-divisible-pairs-in-an-array.cpp
76 lines (73 loc) · 2.16 KB
/
count-equal-and-divisible-pairs-in-an-array.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
// Time: O(nlogk + n * sqrt(k))
// Space: O(n + sqrt(k)), number of factors of k is at most sqrt(k)
// math, number theory
class Solution {
public:
int countPairs(vector<int>& nums, int k) {
unordered_map<int, vector<int>> idxs;
for (int i = 0; i < size(nums); ++i) {
idxs[nums[i]].emplace_back(i);
}
int result = 0;
for (const auto& [_, idx] : idxs) {
unordered_map<int, int> gcds;
for (const auto& i : idx) {
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;
}
};
// Time: O(nlogk + n * sqrt(k)^2) = O(n * k)
// Space: O(n * sqrt(k)), number of factors of k is at most sqrt(k)
// math, number theory
class Solution2 {
public:
int countPairs(vector<int>& nums, int k) {
unordered_map<int, unordered_map<int, int>> cnts;
for (int i = 0; i < size(nums); ++i) {
++cnts[nums[i]][gcd(i, k)];
}
int result = 0;
for (const auto& [_, cnt] : cnts) {
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(n^2)
// Space: O(n)
// brute force
class Solution3 {
public:
int countPairs(vector<int>& nums, int k) {
unordered_map<int, vector<int>> idxs;
for (int i = 0; i < size(nums); ++i) {
idxs[nums[i]].emplace_back(i);
}
int result = 0;
for (const auto& [_, idx] : idxs) {
for (int i = 0; i < size(idx); ++i) {
for (int j = i + 1; j < size(idx); ++j) {
if (idx[i] * idx[j] % k == 0) {
++result;
}
}
}
}
return result;
}
};