1497. Check If Array Pairs Are Divisible by k - Detailed Explanation
Problem Statement
Given an array of integers arr
of even length and an integer k
, determine whether it is possible to rearrange the array into pairs such that the sum of each pair is divisible by k
. In other words, for each pair (a, b)
formed from the elements of arr
, the condition
(a + b) % k == 0
must hold.
Example 1
- Input:
arr = [9, 7, 5, 3]
,k = 6
- Output:
true
- Explanation: One way to form the pairs is
(9, 3)
and(7, 5)
. Their sums are 12 and 12 respectively, and since 12 % 6 = 0, the condition is satisfied.
Example 2
- Input:
arr = [92, 75, 65, 48, 45, 35]
,k = 10
- Output:
true
- Explanation: The remainders when dividing by 10 are:
- 92 % 10 = 2
- 75 % 10 = 5
- 65 % 10 = 5
- 48 % 10 = 8
- 45 % 10 = 5
- 35 % 10 = 5
Here, one possible pairing is to pair the element with remainder 2 (92) with the element with remainder 8 (48), and pair the remaining ones (all with remainder 5) among themselves. Notice that whenk
is even, the numbers that give a remainder ofk/2
(in this case, 5) must have an even count.
Example 3
- Input:
arr = [1, 2, 3, 4, 5, 6]
,k = 10
- Output:
false
- Explanation: The remainders are 1, 2, 3, 4, 5, 6. For a valid pairing, every remainder
r
should have a matching count equal to the count of remainder(k - r)
. Here, there is no remainder 9 to pair with remainder 1 (since 1 + 9 = 10) and similarly for other unmatched values, so it is impossible to form valid pairs.
Constraints:
- The length of
arr
is even. - 1 ≤ arr.length ≤ 10⁵
- -10⁵ ≤ arr[i] ≤ 10⁵
- 1 ≤ k ≤ 10⁵
Hints
-
Modulo Analysis:
Instead of working with the actual numbers, compute the remainder of each element when divided byk
(i.e.,num % k
). Numbers can be grouped by these remainders. -
Pairing Conditions:
For the pairing to be possible:- All elements with remainder
0
must appear an even number of times. - For any remainder
r
(where1 <= r < k
), the count ofr
should equal the count ofk - r
. - If
k
is even, elements with remainderk/2
must also appear an even number of times becausek/2 + k/2 = k
.
- All elements with remainder
Approaches
Approach 1: Remainder Frequency Counting
Explanation
-
Compute Remainder Frequencies:
- Traverse the array and compute
remainder = num % k
for each number. (If handling negatives, adjust the remainder so it lies in the range [0, k–1].) - Use a hash map (or array of size
k
) to count the frequency of each remainder.
- Traverse the array and compute
-
Check for Valid Pairs:
- If the remainder is
0
, ensure that its count is even. - For remainders
r
from 1 tok-1
, check whether the frequency ofr
equals the frequency ofk - r
. - For the special case where
k
is even, verify that the count of elements with remainderk/2
is even.
- If the remainder is
-
Return Result:
If all the above conditions are met, then it is possible to rearrange the array into valid pairs; otherwise, it is not.
Complexity Analysis
- Time Complexity: O(n), where n is the size of
arr
(we perform one pass to count remainders and another pass over possible remainders up to k). - Space Complexity: O(k), for storing the frequency counts.
Approach 2: Two-Pass Verification
A similar method involves two passes:
-
In the first pass, compute and store the frequencies of all remainders.
-
In the second pass, iterate over the possible remainders from 0 to k/2 and check the required conditions for valid pairing.
This approach essentially follows the same idea as Approach 1 with slightly different implementation details.
Code Solutions
Python Implementation
Java Implementation
Step-by-Step Walkthrough and Visual Example
Consider Example 1: arr = [9, 7, 5, 3]
and k = 6
.
- Compute Remainders:
- 9 % 6 = 3
- 7 % 6 = 1
- 5 % 6 = 5
- 3 % 6 = 3
Frequency array (remainderCount
) for remainders from 0 to 5 becomes:
[0, 1, 0, 2, 0, 1]
- Check Special Cases:
- For remainder 0: count is 0 (even).
- For remainder 1: count = 1; check its complement remainder 5: count = 1.
- For remainder 2: count = 0; its complement remainder 4: count = 0.
- For remainder 3: Since 6 is even, when r == k/2 (i.e. 3) the count must be even. Here count = 2 which is even.
- Conclusion:
Since all conditions are met, the array can be paired properly.
Common Mistakes and Edge Cases
-
Handling Negative Numbers:
Negative integers can produce negative remainders in many languages. Always adjust by computingremainder = ((num % k) + k) % k
to ensure the remainder is in the range
[0, k-1]
. -
Forgetting Special Cases:
- The remainder
0
group must have an even count. - When
k
is even, the group with remainderk/2
also requires an even count.
- The remainder
-
Mismatched Frequencies:
If the frequency of a remainderr
does not equal the frequency ofk - r
, pairing is impossible.
Alternative Variations
-
Different Pairing Criteria:
Variations of this problem may include different conditions for pairing (such as products divisible by k instead of sums). The core idea of grouping by modulo results is often still applicable. -
Maximizing or Minimizing Sum:
Some variations might ask you to pair elements to maximize or minimize the sum of the pairs while obeying certain conditions; these require additional greedy or dynamic programming strategies.
Related Problems
GET YOUR FREE
Coding Questions Catalog