Skip to main content
  1. LeetCode/

LeetCode 1497: Check If Array Pairs Are Divisible by k (Remainder Pairing, Modulo)

·2 mins· ·
LeetCode Daily Medium Array Hash Map Modulo Math Counting Complement
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: Medium First Attempt: 2025-08-09

  • Total time: 00:00.00

Intuition
#

Count each value’s remainder modulo k in a hash map and pair remainder r with its complement k − r. The special cases are r = 0 and (when k is even) r = k/2; their counts must be even to form valid pairs.

Approach
#

  • Compute num % k for every element and record counts in a hash map of remainders.
  • For each remainder r, check that count[r] == count[k - r].
  • Special cases:
    • Remainder 0 must have an even count.
    • If k is even, remainder k/2 must also have an even count.
class Solution:
    def canArrange(self, arr: List[int], k: int) -> bool:
        remains = defaultdict(int)

        for num in arr:
            if num % k:
                remains[num % k] += 1

        for remain_num, count in remains.items():
            if k - remain_num == remain_num and remains[k - remain_num] % 2:
                return False

            elif remains[k - remain_num] != remains[remain_num]:
                return False

        return True

Complexity
#

  • Time: O(n)
  • Space: O(k)

Findings
#

  • Complement pairing on remainders is the key idea: r pairs with k − r.
  • Python’s % yields non‑negative remainders, so negatives are handled implicitly.
  • Common pitfalls: forgetting to handle remainder 0 and (if k is even) remainder k/2.

Encountered Problems
#

  • Edge cases to handle explicitly:
    • Numbers divisible by k (remainder 0) must appear an even number of times so they can pair among themselves.
    • When k is even, numbers with remainder k/2 must also appear an even number of times; otherwise, count[k/2] == count[k - k/2] alone can be misleading (e.g., k = 4 and three 2s).

Related

LeetCode 1695: Maximum Erasure Value (Sliding Window)
·1 min
LeetCode Daily Medium Sliding Window Two Pointers Array Hashset
Sliding window with a set and running sum; two pointers to keep the subarray unique.
LeetCode 209: Minimum Size Subarray Sum (Sliding Window)
·1 min
LeetCode Daily Medium Array Sliding Window Two Pointers Prefix Sum Binary-Search
Sliding window with two pointers; minimize subarray length where sum ≥ target.
LeetCode 3439: Reschedule Meetings for Maximum Free Time (Sliding Window)
·3 mins
LeetCode Daily Medium Sliding Window Two Pointers Intervals Array
Sliding window over meetings; track total duration in a k-sized window and compute free span between boundaries.
LeetCode 496: Next Greater Element I (Monotonic Stack)
·2 mins
LeetCode Daily Easy Array Stack Monotonic-Stack Hash Map Next Greater Element
Monotonic decreasing stack over nums2 to build next-greater map; answer queries for nums1.
LeetCode 904: Fruit Into Baskets
·3 mins
LeetCode Daily Medium Sliding Window Hash Map Two Pointers Variable Window
LeetCode Problem Solving - Variable Size Sliding Window with Hash Map
LeetCode 1004: Max Consecutive Ones III
·3 mins
LeetCode Daily Medium Sliding Window Array Two Pointers Binary Array
LeetCode Problem Solving - Variable Size Sliding Window with Zero Counting