The Indian Engineer

Problem 1497 Check If Array Pairs Are Divisible by k

Posted on 7 mins

Array Hash-Table Counting

Problem Statement

Link - Problem 1497

Question

Given an array of integers arr of even length n and an integer k.

We want to divide the array into exactly n / 2 pairs such that the sum of each pair is divisible by k.

Return true If you can find a way to do that or false otherwise.

Example 1:

Input: arr = [1,2,3,4,5,10,6,7,8,9], k = 5
Output: true
Explanation: Pairs are (1,9),(2,8),(3,7),(4,6) and (5,10).

Example 2:

Input: arr = [1,2,3,4,5,6], k = 7
Output: true
Explanation: Pairs are (1,6),(2,5) and(3,4).

Example 3:

Input: arr = [1,2,3,4,5,6], k = 10
Output: false
Explanation: You can try all possible pairs to see that 
there is no way to divide arr into 3 pairs each with sum divisible by 10.

Constraints:

Solution

class Solution {
public:
    bool canArrange(vector<int>& arr, int k) {
        unordered_map<int, int> remainderCount;

        for (int num : arr) {
            int remainder = ((num % k) + k) % k; // This is to get a non negetive reminder
            remainderCount[remainder]++;
        }

        for (auto& pair : remainderCount) {
            int remainder = pair.first;
            int count = pair.second;

            if (remainder == 0) {
                // For remainder 0, there must be an even number of elements
                if (count % 2 != 0) {
                    return false;
                }
            } else if (remainder * 2 == k) {
                // For remainder k/2, there must be an even number of elements
                if (count % 2 != 0) {
                    return false;
                }
            } else {
                // For other remainders, there must be a matching count of k - remainder
                int complement = k - remainder;
                if (remainderCount[complement] != count) {
                    return false;
                }
            }
        }

        return true;
    }
};

Complexity Analysis

| Algorithm  | Time Complexity | Space Complexity |
| ---------- | --------------- | ---------------- |
| Hash Table | O(n)            | O(k)             |

Explanation

Intial Thoughts

To approach this problem, we should first consider how the array can be divided into pairs such that the sum of each pair is divisible by k. We can think of this as finding pairs of numbers that ‘complement’ each other with respect to k. It’s also important to think about how the remainders of the numbers when divided by k can help us find these pairs. We should consider how many numbers in the array have each possible remainder when divided by k, and how these counts can be used to determine if the desired pairs can be formed.

Intuitive Analysis

Intuitively, we can solve this problem by considering the counts of each remainder when the numbers in the array are divided by k. We should think about how the counts of each remainder can be used to determine if there are enough ‘matching’ numbers to form the desired pairs. For example, if we have a certain number of elements with a remainder of r when divided by k, we need to have the same number of elements with a remainder of k - r. We should also consider the special cases where the remainder is 0 or k/2, as these may require an even number of elements. By thinking about these cases and how the counts of each remainder relate to each other, we can develop a solution to the problem.

1. Intuition

2. Implementation


Complexity Analysis

Time Complexity:

Space Complexity:


Footnote

This question is rated as Medium difficulty.

Hints

Keep an array of the frequencies of ((x % k) + k) % k for each x in arr.

for each i in [0, k - 1] we need to check if freq[i] == freq[k - i]

Take care of the case when i == k - i and when i == 0


Similar Questions:

Title URL Difficulty
Count Array Pairs Divisible by K https://leetcode.com/problems/count-array-pairs-divisible-by-k Hard
Minimum Deletions to Make Array Divisible https://leetcode.com/problems/minimum-deletions-to-make-array-divisible Hard
Count Pairs That Form a Complete Day II https://leetcode.com/problems/count-pairs-that-form-a-complete-day-ii Medium
Count Pairs That Form a Complete Day I https://leetcode.com/problems/count-pairs-that-form-a-complete-day-i Easy