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 == n
1 <= n <= 105
n
is even.-109 <= arr[i] <= 109
1 <= k <= 105
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method to solve this involves checking every single possible pairing of numbers. We want to see if, for every pair we pick, their sum is nicely divisible by a given number. This approach guarantees we'll find the answer, albeit slowly, by simply trying all combinations.
Here's how the algorithm would work step-by-step:
def check_if_array_pairs_are_divisible_by_k_brute_force(
numbers, divisor
): # Iterate through all possible pairs in the list
for first_index in range(len(numbers)):
for second_index in range(first_index + 1, len(numbers)): # Calculate the sum of the current pair
current_sum = numbers[first_index] + numbers[second_index]
# If the sum is not divisible, it's impossible
if current_sum % divisor != 0:
return False
# All pairs are divisible by k
return True
The goal is to pair numbers from a list so that the sum of each pair is evenly divisible by a given number. We can avoid checking every possible pair by focusing on how each number behaves when divided by the given number, and pairing remainders.
Here's how the algorithm would work step-by-step:
def check_if_array_pairs_are_divisible_by_k(numbers, divisor):
remainder_counts = {}
for number in numbers:
remainder = number % divisor
if remainder not in remainder_counts:
remainder_counts[remainder] = 0
remainder_counts[remainder] += 1
# Iterate through the remainders to find their complements.
for remainder, count in remainder_counts.items():
complement = (divisor - remainder) % divisor
# Need to treat remainder 0 and k/2 as special cases.
if remainder == 0 or remainder == divisor / 2:
if count % 2 != 0:
return False
# If remainder and complement counts do not match, it is impossible to pair
elif remainder_counts.get(complement, 0) != count:
return False
return True
Case | How to Handle |
---|---|
Null or empty input array | Return true if the array is null or empty, as it trivially satisfies the condition (no pairs to check). |
Array with an odd number of elements | Return false if the array has an odd number of elements, as it is impossible to form pairs. |
k is zero | Throw an IllegalArgumentException or return false, as division by zero is undefined and no sum can be divisible by zero. |
Array with all elements equal to 0 and k != 0 | Return true if all elements are 0, since the sum of any pair will be 0, which is divisible by any non-zero k. |
Array with mixed positive and negative numbers | Ensure the modulo operation handles negative numbers correctly (e.g., by adding k until the result is non-negative) when calculating remainders. |
Large input array with potential integer overflow when summing pairs | The problem specifies sums divisible by k, not the sum itself needing to be stored, so overflow is less of a concern, and using long to store sums temporarily mitigates risks. |
Array contains duplicate values that are modular inverses of each other | The hashmap stores counts of each remainder and ensures there are enough pairs of each modular inverse. |
k is a large prime number and the array contains large numbers | The modulo operation mitigates issues of large numbers and prime k values, as we are concerned with remainders, not the large number itself. |