Check If Array Pairs Are Divisible by k
MEDIUMDescription
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.
Example 1:
Input: arr = [1,2,3,4,5,10,6,7,8,9], k = 5 Output: true Explanation: Pairs are (1,9),(2,8),(3,7),(4,6) and (5,10).
Example 2:
Input: arr = [1,2,3,4,5,6], k = 7 Output: true Explanation: Pairs are (1,6),(2,5) and(3,4).
Example 3:
Input: arr = [1,2,3,4,5,6], k = 10 Output: false Explanation: You can try all possible pairs to see that there is no way to divide arr into 3 pairs each with sum divisible by 10.
Constraints:
arr.length == n1 <= n <= 105nis even.-109 <= arr[i] <= 1091 <= k <= 105
Approaches
Checkout 2 different approaches to solve Check If Array Pairs Are Divisible by k. Click on different approaches to view the approach and algorithm in detail.
Optimal Approach using Frequency of Remainders
A much more efficient and optimal approach leverages modular arithmetic. The key insight is that for a pair (a, b) to have a sum divisible by k, their remainders must complement each other. That is, (a % k + b % k) % k == 0. This implies that for an element with remainder r, its partner must have a remainder of k - r (or 0 if r is 0). Instead of trying to pair actual numbers, we can just count the frequencies of their remainders and check if these counts allow for a valid pairing.
Algorithm
- Create an integer array
remainderCountsof sizek, initialized to all zeros. This array will store the frequency of each possible remainder. - Iterate through each number
numin the input arrayarr. - For each
num, calculate its remainder modulok. To handle negative numbers correctly and ensure the remainder is in the range[0, k-1], use the formularem = ((num % k) + k) % k. - Increment the count for this remainder:
remainderCounts[rem]++. - After populating the frequency array, check the pairing conditions:
- Check
remainderCounts[0]. Since elements with a remainder of 0 can only be paired with other elements with a remainder of 0, their total count must be even. IfremainderCounts[0] % 2 != 0, returnfalse. - Iterate from
r = 1up tok / 2. - For each
r, ifr * 2 == k(which only happens ifkis even andr = k/2), these elements must pair with each other. Thus,remainderCounts[r]must be even. If not, returnfalse. - Otherwise (if
r != k-r), an element with remainderrmust pair with an element with remainderk-r. Therefore, their counts must be equal. IfremainderCounts[r] != remainderCounts[k - r], returnfalse.
- Check
- If all conditions are met after checking all remainders, it means a valid pairing is possible. Return
true.
This method transforms the problem from pairing numbers to pairing remainders. We don't care about the numbers themselves, only their remainders when divided by k.
The algorithm proceeds in two main phases:
-
Counting Remainders: We iterate through the entire input array
arronce. For each number, we compute its remainder modulok. A crucial detail is handling potentially negative numbers inarr. The standard%operator in Java can yield negative results (e.g.,-1 % 5is-1). To map all remainders to the non-negative range[0, k-1], we use the expression((num % k) + k) % k. We use an array of sizekto store the frequency of each of these non-negative remainders. -
Verifying Frequencies: After counting, we check if the frequencies allow for perfect pairing.
- Remainder 0: An element
xwithx % k == 0needs a partnerywithy % k == 0. This means all elements divisible bykmust be paired amongst themselves. Therefore, the total count of such elements (remainderCounts[0]) must be even. - Complementary Remainders: An element
xwith remainderr(wherer != 0) needs a partnerywith remainderk - r. This means the number of elements with remainderrmust exactly match the number of elements with remainderk - r. - Special Case
r = k/2: Ifkis an even number, a special case arises for the remainderr = k / 2. Here,k - r = r, so an element with this remainder must be paired with another element that also has the same remainder. Consequently, the count of elements with remainderk/2must be even.
- Remainder 0: An element
If all these conditions hold, we can form the pairs, so we return true. Otherwise, it's impossible, and we return false.
class Solution {
public boolean canArrange(int[] arr, int k) {
int[] remainderCounts = new int[k];
for (int num : arr) {
int rem = ((num % k) + k) % k;
remainderCounts[rem]++;
}
// Condition 1: Count of numbers with remainder 0 must be even.
if (remainderCounts[0] % 2 != 0) {
return false;
}
// Condition 2: For r from 1 to k/2, check complementary pairs.
for (int r = 1; r <= k / 2; r++) {
// If r is its own complement (only when k is even and r = k/2)
if (r * 2 == k) {
if (remainderCounts[r] % 2 != 0) {
return false;
}
} else {
// For all other r, count(r) must equal count(k-r).
if (remainderCounts[r] != remainderCounts[k - r]) {
return false;
}
}
}
return true;
}
}
Complexity Analysis
Pros and Cons
- Highly efficient with linear time complexity.
- Optimal solution for the given constraints.
- Handles all edge cases, including negative numbers, correctly and elegantly.
- Requires extra space proportional to
k, which could be large ifkis very large (though it's fine for the given constraints).
Code Solutions
Checking out 4 solutions in different languages for Check If Array Pairs Are Divisible by k. Click on different languages to view the code.
class Solution {
public
boolean canArrange(int[] arr, int k) {
int[] cnt = new int[k];
for (int x : arr) {
++cnt[(x % k + k) % k];
}
for (int i = 1; i < k; ++i) {
if (cnt[i] != cnt[k - i]) {
return false;
}
}
return cnt[0] % 2 == 0;
}
}
Video Solution
Watch the video walkthrough for Check If Array Pairs Are Divisible by k
Similar Questions
5 related questions you might find useful
Subscribe to Scale Engineer newsletter
Learn about System Design, Software Engineering, and interview experiences every week.
No spam, unsubscribe at any time.