Skip to content
LC-1497 Medium LeetCode

1497. Check If Array Pairs Are Divisible by k

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 46% Topics: Array, Hash Table, Counting
View full problem on LeetCode

Reading material

Reference solution (spoiler · python)
# Time:  O(n)
# Space: O(k)

import collections


class Solution(object):
    def canArrange(self, arr, k):
        """
        :type arr: List[int]
        :type k: int
        :rtype: bool
        """
        count = collections.Counter(i%k for i in arr)
        return (0 not in count or not count[0]%2) and \
                all(k-i in count and count[i] == count[k-i] for i in xrange(1, k) if i in count)

Solution from kamyu104/LeetCode-Solutions · MIT