-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path1497.cpp
More file actions
35 lines (33 loc) · 908 Bytes
/
1497.cpp
File metadata and controls
35 lines (33 loc) · 908 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
// Author: btjanaka (Bryon Tjanaka)
// Problem: (LeetCode) 1497
// Title: Check If Array Pairs Are Divisible by k
// Link: https://leetcode.com/problems/check-if-array-pairs-are-divisible-by-k/
// Idea: Mod each number by k and count the frequencies of the modulos.
// Difficulty: Medium
// Tags:
class Solution {
public:
bool canArrange(vector<int>& arr, int k) {
unordered_map<int, int> mods;
for (int x : arr) {
int m = x % k;
if (m < 0) m += k;
if (mods.find(m) == mods.end()) mods[m] = 0;
++mods[m];
}
bool match = true;
for (auto p : mods) {
int m = p.first, count = p.second;
if (m == 0) {
match = count % 2 == 0;
} else {
int complement = k - m;
if (mods.find(complement) == mods.end() || mods[complement] != count) {
match = false;
break;
}
}
}
return match;
}
};