Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Check if an array can be divided into pairs whose sum is divisible by k in Python
Given an array of integers and a number k, we need to determine whether it's possible to divide the entire array into pairs such that the sum of every pair is divisible by k.
For example, with array [2, 4, 1, 3] and k = 5:
Pair (2, 3) ? 2 + 3 = 5 (divisible by 5) Pair (4, 1) ? 4 + 1 = 5 (divisible by 5) Result: True (all pairs sum to multiples of k)
Algorithm
The key insight is that for two numbers to sum to a multiple of k, their remainders when divided by k must complement each other ?
- Step 1: Check if array length is even (pairs require even count)
- Step 2: Count remainder frequencies for each element modulo k
- Step 3: For remainder 0, count must be even (pairs within remainder 0)
- Step 4: For other remainders, remainder r must have same count as remainder (k-r)
Using Remainder Frequency Count
Example 1: Valid Pairing
Let's check if array [2, 4, 1, 3] with k=5 can form valid pairs ?
from collections import defaultdict
def can_divide_into_pairs(array, k):
# Step 1: Check if array length is even
if len(array) % 2 != 0:
return False
# Step 2: Count remainders
remainder_count = defaultdict(int)
# Step 3: Calculate remainder frequency
for num in array:
remainder = num % k
remainder_count[remainder] += 1
# Step 4: Check pairing conditions
for remainder in remainder_count:
complement = (k - remainder) % k
# Special case: remainder 0 must have even count
if remainder == 0:
if remainder_count[remainder] % 2 != 0:
return False
# General case: remainder and complement must have equal counts
else:
if remainder_count[remainder] != remainder_count[complement]:
return False
return True
# Test with valid array
array = [2, 4, 1, 3]
k = 5
result = can_divide_into_pairs(array, k)
print(f"Array: {array}, k: {k}")
print(f"Can divide into pairs: {result}")
# Show remainder analysis
remainder_count = defaultdict(int)
for num in array:
remainder_count[num % k] += 1
print(f"Remainder counts: {dict(remainder_count)}")
Array: [2, 4, 1, 3], k: 5
Can divide into pairs: True
Remainder counts: {2: 1, 4: 1, 1: 1, 3: 1}
Example 2: Invalid Pairing (Odd Length)
Testing with an odd-length array that cannot form complete pairs ?
# Test with odd-length array
array = [1, 2, 3, 4, 5]
k = 4
result = can_divide_into_pairs(array, k)
print(f"Array: {array}, k: {k}")
print(f"Can divide into pairs: {result}")
print(f"Reason: Array length {len(array)} is odd")
Array: [1, 2, 3, 4, 5], k: 4 Can divide into pairs: False Reason: Array length 5 is odd
Example 3: Invalid Pairing (Unbalanced Remainders)
Testing with unbalanced remainder distribution ?
# Test with unbalanced remainders
array = [1, 1, 2, 2, 3, 4]
k = 5
result = can_divide_into_pairs(array, k)
print(f"Array: {array}, k: {k}")
print(f"Can divide into pairs: {result}")
# Show remainder analysis
remainder_count = defaultdict(int)
for num in array:
remainder_count[num % k] += 1
print(f"Remainder counts: {dict(remainder_count)}")
print("Analysis: Remainder 1 has count 2, but complement 4 has count 1")
Array: [1, 1, 2, 2, 3, 4], k: 5
Can divide into pairs: False
Remainder counts: {1: 2, 2: 2, 3: 1, 4: 1}
Analysis: Remainder 1 has count 2, but complement 4 has count 1
How It Works
The algorithm works by analyzing remainder patterns:
- Remainder 0: Elements divisible by k can only pair with other remainder-0 elements
- Remainder r: Must pair with remainder (k-r) to sum to a multiple of k
- Balance requirement: Each remainder must have exactly as many elements as its complement
Time and Space Complexity
| Metric | Complexity | Explanation |
|---|---|---|
| Time | O(n) | Single pass to count remainders |
| Space | O(k) | Dictionary stores at most k different remainders |
Conclusion
Use remainder frequency counting to efficiently check if an array can be divided into pairs with sums divisible by k. The key insight is that remainders must be balanced with their complements for valid pairing.
