[
counter
math
]
BinarySearch 0644 Partition List to Pairs that Are Divisible by K
Problem statement
https://binarysearch.com/problems/Partition-List-to-Pairs-that-Are-Divisible-by-K/
Solution
For each reminder calculate number of values we have, then check cases 0
and k//2
.
Complexity
It is O(n)
for time and space.
Code
class Solution:
def solve(self, nums, k):
cnt = Counter()
for num in nums:
cnt[num%k] += 1
for i in cnt:
if i == 0 or i*2 == k:
if cnt[i] % 2 == 1: return False
else:
if cnt[i] != cnt[k - i]: return False
return True