Skip to content

Commit e99a68f

Browse files
authored
Create check-if-array-pairs-are-divisible-by-k.cpp
1 parent 35c7b9e commit e99a68f

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Time: O(n)
2+
// Space: O(k)
3+
4+
class Solution {
5+
public:
6+
bool canArrange(vector<int>& arr, int k) {
7+
if (arr.size() % 2) {
8+
return false;
9+
}
10+
unordered_map<int, int> count;
11+
for (const auto& i : arr) {
12+
++count[(i % k + k) % k];
13+
}
14+
for (int i = 0; i < k; ++i) {
15+
if ((!i && count[i] % 2) ||
16+
(i && count[i] != count[k - i])) {
17+
return false;
18+
}
19+
}
20+
return true;
21+
}
22+
};

0 commit comments

Comments
 (0)