← Home

1497. Check If Array Pairs Are Divisible by k

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
greedyC++Markdown
149

The trick here is to name the state correctly, then let the implementation follow. For 1497. Check If Array Pairs Are Divisible by k, the solution in this repository is mainly a greedy solution.

Guide

What?

We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: greedy.

Guide

When?

This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are canArrange, remainderFreq.

Guide

Why?

The solution works because it narrows the problem until every update has a clear reason to exist.

  • Sorting is used to make local choices comparable, so the later scan does not have to rediscover order.
  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Read the setup variables first.
  2. Follow the main loop or recursive helper next.
  3. Watch where invalid states get skipped.
  4. Check which value survives to the return statement.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Runtime: 584 ms, faster than 15.78% of C++ online submissions for Check If Array Pairs Are Divisible by k.
02//Memory Usage: 60.4 MB, less than 100.00% of C++ online submissions for Check If Array Pairs Are Divisible by k.
03class Solution {
04public:
05    bool canArrange(vector<int>& arr, int k) {
06        for(int& num : arr){
07            num = (num%k + k) % k;
08        }
09        
10        int zeroCount = count_if(arr.begin(), arr.end(), [](int num){return num == 0;});
11        if(zeroCount % 2 != 0) return false;
12        
13        arr.erase(remove(arr.begin(), arr.end(), 0), arr.end());
14        
15        sort(arr.begin(), arr.end());
16        
17        // for(int& num : arr){
18        //     cout << num << " ";
19        // }
20        // cout << endl;
21        
22        int n = arr.size();
23        
24        for(int i = 0; i < n/2; ++i){
25            // cout << i << " and " << n-i-1 << " : " << arr[i] << " and " << arr[n-1-i] << endl;
26            if((arr[i] + arr[n-1-i])%k != 0){
27                return false;
28            }
29        }
30        
31        return true;
32    }
33};
34
35//remainder frequency
36//https://leetcode.com/problems/check-if-array-pairs-are-divisible-by-k/discuss/709331/Weak-TC-passes-my-1-liner-or-Correct-Solution-Using-Reminder-Frequency
37//Runtime: 272 ms, faster than 71.67% of C++ online submissions for Check If Array Pairs Are Divisible by k.
38//Memory Usage: 61.8 MB, less than 100.00% of C++ online submissions for Check If Array Pairs Are Divisible by k.
39class Solution {
40public:
41    bool canArrange(vector<int>& arr, int k) {
42        vector<int> remainderFreq(k);
43        
44        for(int& num : arr){
45            ++remainderFreq[(num%k+k)%k];
46        }
47        
48        if(remainderFreq[0] % 2 != 0)
49            return false;
50        
51        for(int remainder = 1; remainder < k - remainder; ++remainder){
52            if(remainderFreq[remainder] != remainderFreq[k-remainder]){
53                return false;
54            }
55        }
56        
57        return true;
58    }
59};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.