1497. Check If Array Pairs Are Divisible by k - Detailed Explanation

Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!

Problem Statement

Given an array of integers arr of even length and an integer k, determine whether it is possible to rearrange the array into pairs such that the sum of each pair is divisible by k. In other words, for each pair (a, b) formed from the elements of arr, the condition

(a + b) % k == 0

must hold.

Example 1

  • Input: arr = [9, 7, 5, 3], k = 6
  • Output: true
  • Explanation: One way to form the pairs is (9, 3) and (7, 5). Their sums are 12 and 12 respectively, and since 12 % 6 = 0, the condition is satisfied.

Example 2

  • Input: arr = [92, 75, 65, 48, 45, 35], k = 10
  • Output: true
  • Explanation: The remainders when dividing by 10 are:
    • 92 % 10 = 2
    • 75 % 10 = 5
    • 65 % 10 = 5
    • 48 % 10 = 8
    • 45 % 10 = 5
    • 35 % 10 = 5
      Here, one possible pairing is to pair the element with remainder 2 (92) with the element with remainder 8 (48), and pair the remaining ones (all with remainder 5) among themselves. Notice that when k is even, the numbers that give a remainder of k/2 (in this case, 5) must have an even count.

Example 3

  • Input: arr = [1, 2, 3, 4, 5, 6], k = 10
  • Output: false
  • Explanation: The remainders are 1, 2, 3, 4, 5, 6. For a valid pairing, every remainder r should have a matching count equal to the count of remainder (k - r). Here, there is no remainder 9 to pair with remainder 1 (since 1 + 9 = 10) and similarly for other unmatched values, so it is impossible to form valid pairs.

Constraints:

  • The length of arr is even.
  • 1 ≤ arr.length ≤ 10⁵
  • -10⁵ ≤ arr[i] ≤ 10⁵
  • 1 ≤ k ≤ 10⁵

Hints

  1. Modulo Analysis:
    Instead of working with the actual numbers, compute the remainder of each element when divided by k (i.e., num % k). Numbers can be grouped by these remainders.

  2. Pairing Conditions:
    For the pairing to be possible:

    • All elements with remainder 0 must appear an even number of times.
    • For any remainder r (where 1 <= r < k), the count of r should equal the count of k - r.
    • If k is even, elements with remainder k/2 must also appear an even number of times because k/2 + k/2 = k.

Approaches

Approach 1: Remainder Frequency Counting

Explanation

  1. Compute Remainder Frequencies:

    • Traverse the array and compute remainder = num % k for each number. (If handling negatives, adjust the remainder so it lies in the range [0, k–1].)
    • Use a hash map (or array of size k) to count the frequency of each remainder.
  2. Check for Valid Pairs:

    • If the remainder is 0, ensure that its count is even.
    • For remainders r from 1 to k-1, check whether the frequency of r equals the frequency of k - r.
    • For the special case where k is even, verify that the count of elements with remainder k/2 is even.
  3. Return Result:
    If all the above conditions are met, then it is possible to rearrange the array into valid pairs; otherwise, it is not.

Complexity Analysis

  • Time Complexity: O(n), where n is the size of arr (we perform one pass to count remainders and another pass over possible remainders up to k).
  • Space Complexity: O(k), for storing the frequency counts.

Approach 2: Two-Pass Verification

A similar method involves two passes:

  1. In the first pass, compute and store the frequencies of all remainders.

  2. In the second pass, iterate over the possible remainders from 0 to k/2 and check the required conditions for valid pairing.

This approach essentially follows the same idea as Approach 1 with slightly different implementation details.

Code Solutions

Python Implementation

Python3
Python3

. . . .

Java Implementation

Java
Java

. . . .

Step-by-Step Walkthrough and Visual Example

Consider Example 1: arr = [9, 7, 5, 3] and k = 6.

  1. Compute Remainders:
    • 9 % 6 = 3
    • 7 % 6 = 1
    • 5 % 6 = 5
    • 3 % 6 = 3
      Frequency array (remainderCount) for remainders from 0 to 5 becomes:
    [0, 1, 0, 2, 0, 1]
    
  2. Check Special Cases:
    • For remainder 0: count is 0 (even).
    • For remainder 1: count = 1; check its complement remainder 5: count = 1.
    • For remainder 2: count = 0; its complement remainder 4: count = 0.
    • For remainder 3: Since 6 is even, when r == k/2 (i.e. 3) the count must be even. Here count = 2 which is even.
  3. Conclusion:
    Since all conditions are met, the array can be paired properly.

Common Mistakes and Edge Cases

  • Handling Negative Numbers:
    Negative integers can produce negative remainders in many languages. Always adjust by computing

    remainder = ((num % k) + k) % k
    

    to ensure the remainder is in the range [0, k-1].

  • Forgetting Special Cases:

    • The remainder 0 group must have an even count.
    • When k is even, the group with remainder k/2 also requires an even count.
  • Mismatched Frequencies:
    If the frequency of a remainder r does not equal the frequency of k - r, pairing is impossible.

Alternative Variations

  • Different Pairing Criteria:
    Variations of this problem may include different conditions for pairing (such as products divisible by k instead of sums). The core idea of grouping by modulo results is often still applicable.

  • Maximizing or Minimizing Sum:
    Some variations might ask you to pair elements to maximize or minimize the sum of the pairs while obeying certain conditions; these require additional greedy or dynamic programming strategies.

TAGS
leetcode
CONTRIBUTOR
Design Gurus Team
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
Related Courses
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
Image
One-Stop Portal For Tech Interviews.
Copyright © 2025 Design Gurus, LLC. All rights reserved.
;