Check If Array Pairs Are Divisible by k LeetCode solution in C++

1
// ask the interviewer
2
// can we have negative values?
3

4
class Solution {
5
public:
6
bool canArrange(vector<int> &arr, int k) {
7
unordered_map<int, int> memo; // remainder : occurence
8

9
// we are storing the frequency of curr%k
10
// i.e. the frequency of the remainder
11
for (auto curr : arr) {
12
int remainder = ((curr % k) + k) % k;
13
memo[remainder]++;
14
}
15

16
for (int i = 1; i <= k / 2; i++) {
17
if (memo[i] != memo[k - i]) return false;
18
}
19
if (memo[0] % 2 != 0) return false;
20
return true;
21
}
22
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0