1497. Check If Array Pairs Are Divisible by k
Given an array of integers arr of even length n and an integer k.
We want to divide the array into exactly n / 2 pairs such that the sum of each pair is divisible by k.
Return true If you can find a way to do that or false otherwise.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| class Solution { public: bool canArrange(vector<int>& arr, int k) { unordered_map<int, int> freq; for(auto& a : arr) { freq[((a % k) + k) % k]++; } for(auto& [remainder, count] : freq) { if(remainder == 0) { if(count & 1) return false; } else if(remainder * 2 == k) { if(count & 1) return false; } else { if(freq[k - remainder] != count) return false; } } return true; } };
|