All prompts are owned by LeetCode. To view the prompt, click the title link above.
First completed : October 01, 2024
Last updated : October 01, 2024
Related Topics : Array, Hash Table, Counting
Acceptance Rate : 46.28 %
class Solution:
def canArrange(self, arr: List[int], k: int) -> bool:
if k == 1 :
return True
# Count the remainders after mods
remainders = [0] * k
for num in arr :
remainders[num % k] += 1
return all(remainders[x] == remainders[k - x] for x in range(1, k // 2)) and \
((k % 2 == 1 and remainders[k // 2] == remainders[ceil(k / 2)]) or
(k % 2 == 0 and remainders[k // 2] % 2 == 0))