Check If Array Pairs Are Divisible by k (LeetCode 1497): Solution and Explanation
Decide whether an even-length array can be split into pairs whose sums are all multiples of k.
Intuition
Whether a + b is a multiple of k depends only on the remainders of a and b. The actual values do not matter once you know a mod k and b mod k.
Two remainders work together when they add up to k, like 2 and 3 for k = 5, or when both are 0. So every number with remainder r needs a partner with remainder k - r.
That turns pairing into counting. Put each number in its remainder bucket. A valid split exists exactly when bucket r and bucket k - r hold the same count for every r, and bucket 0 holds an even count.
Approach: Remainder buckets
- Create cnt of size k. For each x, add 1 to cnt[((x mod k) + k) mod k]. The extra + k keeps negative remainders in 0 to k - 1.
- If cnt[0] is odd, return false: multiples of k can only pair with each other.
- For r from 1 to k / 2, if cnt[r] differs from cnt[k - r], return false.
- Otherwise return true. When k is even, bucket k / 2 pairs with itself; its count is even automatically once everything else balances, because n is even.
Step-by-step example
arr = [1,2,3,4,5,10,6,7,8,9], k = 5
- Remainders: 1, 2, 3, 4, 0, 0, 1, 2, 3, 4.
- Buckets: cnt[0] = 2, cnt[1] = 2, cnt[2] = 2, cnt[3] = 2, cnt[4] = 2.
- cnt[0] = 2 is even, so 5 and 10 pair with each other.
- r = 1: cnt[1] = cnt[4] = 2. r = 2: cnt[2] = cnt[3] = 2.
- Every bucket matches its partner, so return true.
Code: Python, Java, C++
1class Solution:
2 def canArrange(self, arr: List[int], k: int) -> bool:
3 cnt = [0] * k
4 for x in arr:
5 cnt[x % k] += 1
6 if cnt[0] % 2 != 0:
7 return False
8 for r in range(1, k // 2 + 1):
9 if cnt[r] != cnt[k - r]:
10 return False
11 return True1class Solution {
2 public boolean canArrange(int[] arr, int k) {
3 int[] cnt = new int[k];
4 for (int x : arr) {
5 cnt[((x % k) + k) % k]++;
6 }
7 if (cnt[0] % 2 != 0) {
8 return false;
9 }
10 for (int r = 1; r <= k / 2; r++) {
11 if (cnt[r] != cnt[k - r]) {
12 return false;
13 }
14 }
15 return true;
16 }
17}1class Solution {
2public:
3 bool canArrange(vector<int>& arr, int k) {
4 vector<int> cnt(k, 0);
5 for (int x : arr) {
6 cnt[((x % k) + k) % k]++;
7 }
8 if (cnt[0] % 2 != 0) {
9 return false;
10 }
11 for (int r = 1; r <= k / 2; r++) {
12 if (cnt[r] != cnt[k - r]) {
13 return false;
14 }
15 }
16 return true;
17 }
18};Time and space complexity
O(n + k)
One pass to fill the buckets and one pass over half of the k remainders.
O(k)
One counter per remainder. A hash map of only the remainders that appear uses O(min(n, k)).
Other approaches
Sort by remainder and use two pointers (time O(n log n), space O(n))
Sort the remainders, then match the smallest nonzero remainder with the largest. Works but costs O(n log n) and is easier to get wrong with zeros.
Common mistakes
- Using x % k directly for negative numbers in Java, C++, C#, Go or JavaScript. -1 % 5 is -1 there, not 4, and indexing with it fails or lands in the wrong bucket.
- Forgetting that remainder 0 pairs with itself, so its count must be even rather than equal to some other bucket.
- Trying to build the actual pairs greedily. Counting is enough, because any two numbers in matching buckets work.
Frequently asked questions
Why is it enough to compare bucket counts?
Any number with remainder r can pair with any number with remainder k - r. If the two buckets have equal size, you can match them one to one in any order.
What about remainder k / 2 when k is even?
Those numbers pair with each other, so their count must be even. Because n is even and every other bucket is balanced, that count is always even, so the loop does not need a separate check.
How do I handle negative numbers?
Use ((x % k) + k) % k. It maps every integer to a remainder in 0 to k - 1, which is what Python's % already does.