-
Notifications
You must be signed in to change notification settings - Fork 13
/
solution.py
22 lines (19 loc) · 870 Bytes
/
solution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution:
def canArrange(self, arr: List[int], k: int) -> bool:
# Frequency array to store the count of remainders
remainderFreq = [0] * k
# Step 1: Calculate the remainder for each element and store the frequency
for num in arr:
remainder = (num % k + k) % k # Ensure non-negative remainder
remainderFreq[remainder] += 1
# Step 2: Check if the pairing condition holds
for i in range(k // 2 + 1):
if i == 0:
# Elements with remainder 0 must pair among themselves
if remainderFreq[i] % 2 != 0:
return False
else:
# Remainder i must pair with remainder k-i
if remainderFreq[i] != remainderFreq[k - i]:
return False
return True