|
| 1 | +--- |
| 2 | +id: check-if-array-pairs-are-divisible-by-k |
| 3 | +title: Check If Array Pairs Are Divisible by k |
| 4 | +sidebar_label: 1497. Check If Array Pairs Are Divisible by k |
| 5 | + |
| 6 | +tags: |
| 7 | +- Array |
| 8 | +- Sliding Window |
| 9 | +- Hashmap |
| 10 | + |
| 11 | +description: "This is a solution to the Check If Array Pairs Are Divisible by k problem on LeetCode." |
| 12 | +--- |
| 13 | + |
| 14 | +## Problem Description |
| 15 | +Given an array of integers arr of even length n and an integer k. |
| 16 | +We want to divide the array into exactly n / 2 pairs such that the sum of each pair is divisible by k. |
| 17 | +Return true If you can find a way to do that or false otherwise. |
| 18 | + |
| 19 | +### Examples |
| 20 | + |
| 21 | +**Example 1:** |
| 22 | +``` |
| 23 | +Input: arr = [1,2,3,4,5,10,6,7,8,9], k = 5 |
| 24 | +Output: true |
| 25 | +Explanation: Pairs are (1,9),(2,8),(3,7),(4,6) and (5,10). |
| 26 | +``` |
| 27 | + |
| 28 | +**Example 2:** |
| 29 | +``` |
| 30 | +Input: arr = [1,2,3,4,5,6], k = 7 |
| 31 | +Output: true |
| 32 | +Explanation: Pairs are (1,6),(2,5) and(3,4). |
| 33 | +``` |
| 34 | + |
| 35 | +**Example 3:** |
| 36 | +``` |
| 37 | +Input: arr = [1,2,3,4,5,6], k = 10 |
| 38 | +Output: false |
| 39 | +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. |
| 40 | +``` |
| 41 | +### Constraints |
| 42 | +- `arr.length == n` |
| 43 | +- `1 <= n <= 10^5` |
| 44 | +- n is even. |
| 45 | +- `-10^9 <= arr[i] <= 10^9` |
| 46 | +- `1 <= k <= 10^5` |
| 47 | + |
| 48 | +## Solution for Subarray Sums Divisible by K Problem |
| 49 | +### Approach |
| 50 | +#### Calculate Remainders: |
| 51 | + |
| 52 | +- For each number in the array, calculate its remainder when divided by k. |
| 53 | +- Adjust the remainder to be non-negative using the formula ((num % k) + k) % k. |
| 54 | +#### Count Remainders: |
| 55 | + |
| 56 | +- Use a hash map (or dictionary) to count the frequency of each remainder. |
| 57 | +#### Check Pairs: |
| 58 | + |
| 59 | +- For each unique remainder, check if there exists a complement remainder such that their sums are divisible by k. |
| 60 | + |
| 61 | +#### Special Cases: |
| 62 | +- If the remainder is 0, there must be an even number of such elements because each must pair with another 0 to be divisible by k. |
| 63 | +- For other remainders, ensure that the count of the remainder is equal to the count of its complement (k - remainder). |
| 64 | +#### Return the Result: |
| 65 | +- If all conditions are satisfied, return true. Otherwise, return false. |
| 66 | +<Tabs> |
| 67 | + <TabItem value="Solution" label="Solution"> |
| 68 | + |
| 69 | + #### Implementation |
| 70 | + ```jsx live |
| 71 | + function Solution(arr) { |
| 72 | + var canArrange = function(arr, k) { |
| 73 | + const mp = new Map(); |
| 74 | + for (let num of arr) { |
| 75 | + let remainder = ((num % k) + k) % k; |
| 76 | + mp.set(remainder, (mp.get(remainder) || 0) + 1); |
| 77 | + } |
| 78 | + |
| 79 | + for (let [remainder, count] of mp.entries()) { |
| 80 | + if (remainder === 0) { |
| 81 | + if (count % 2 !== 0) return false; |
| 82 | + } else { |
| 83 | + let complement = k - remainder; |
| 84 | + if (mp.get(remainder) !== mp.get(complement)) { |
| 85 | + return false; |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + return true; |
| 90 | + }; |
| 91 | + |
| 92 | + const input = [1,2,3,4,5,10,6,7,8,9] |
| 93 | + const k = 5 |
| 94 | + const output = canArrange(input , k) |
| 95 | + return ( |
| 96 | + <div> |
| 97 | + <p> |
| 98 | + <b>Input: </b> |
| 99 | + {JSON.stringify(input)} |
| 100 | + </p> |
| 101 | + <p> |
| 102 | + <b>Output:</b> {output.toString()} |
| 103 | + </p> |
| 104 | + </div> |
| 105 | + ); |
| 106 | + } |
| 107 | + ``` |
| 108 | + |
| 109 | + #### Complexity Analysis |
| 110 | + |
| 111 | + - Time Complexity: $ O(n) $ |
| 112 | + - Space Complexity: $ O(k)$ |
| 113 | + |
| 114 | + ## Code in Different Languages |
| 115 | + <Tabs> |
| 116 | + <TabItem value="JavaScript" label="JavaScript"> |
| 117 | + <SolutionAuthor name="@hiteshgahanolia"/> |
| 118 | + ```javascript |
| 119 | + var canArrange = function(arr, k) { |
| 120 | + const mp = new Map(); |
| 121 | + for (let num of arr) { |
| 122 | + let remainder = ((num % k) + k) % k; |
| 123 | + mp.set(remainder, (mp.get(remainder) || 0) + 1); |
| 124 | + } |
| 125 | + |
| 126 | + for (let [remainder, count] of mp.entries()) { |
| 127 | + if (remainder === 0) { |
| 128 | + if (count % 2 !== 0) return false; |
| 129 | + } else { |
| 130 | + let complement = k - remainder; |
| 131 | + if (mp.get(remainder) !== mp.get(complement)) { |
| 132 | + return false; |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + return true; |
| 137 | +}; |
| 138 | + |
| 139 | + ``` |
| 140 | + |
| 141 | + </TabItem> |
| 142 | + <TabItem value="TypeScript" label="TypeScript"> |
| 143 | + <SolutionAuthor name="@hiteshgahanolia"/> |
| 144 | + ```typescript |
| 145 | + function canArrange(arr: number[], k: number): boolean { |
| 146 | + const mp: Map<number, number> = new Map(); |
| 147 | + for (let num of arr) { |
| 148 | + let remainder = ((num % k) + k) % k; |
| 149 | + mp.set(remainder, (mp.get(remainder) || 0) + 1); |
| 150 | + } |
| 151 | +
|
| 152 | + for (let [remainder, count] of mp.entries()) { |
| 153 | + if (remainder === 0) { |
| 154 | + if (count % 2 !== 0) return false; |
| 155 | + } else { |
| 156 | + let complement = k - remainder; |
| 157 | + if (mp.get(remainder) !== mp.get(complement)) { |
| 158 | + return false; |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + return true; |
| 163 | +} |
| 164 | +
|
| 165 | + ``` |
| 166 | + </TabItem> |
| 167 | + <TabItem value="Python" label="Python"> |
| 168 | + <SolutionAuthor name="@hiteshgahanolia"/> |
| 169 | + ```python |
| 170 | + from collections import defaultdict |
| 171 | +
|
| 172 | +class Solution: |
| 173 | + def canArrange(self, arr, k): |
| 174 | + mp = defaultdict(int) |
| 175 | + for num in arr: |
| 176 | + remainder = ((num % k) + k) % k |
| 177 | + mp[remainder] += 1 |
| 178 | +
|
| 179 | + for remainder, count in mp.items(): |
| 180 | + if remainder == 0: |
| 181 | + if count % 2 != 0: |
| 182 | + return False |
| 183 | + else: |
| 184 | + complement = k - remainder |
| 185 | + if mp[remainder] != mp[complement]: |
| 186 | + return False |
| 187 | + return True |
| 188 | +
|
| 189 | + ``` |
| 190 | + |
| 191 | + </TabItem> |
| 192 | + <TabItem value="Java" label="Java"> |
| 193 | + <SolutionAuthor name="@hiteshgahanolia"/> |
| 194 | + ```java |
| 195 | + import java.util.HashMap; |
| 196 | +import java.util.Map; |
| 197 | +
|
| 198 | +class Solution { |
| 199 | + public boolean canArrange(int[] arr, int k) { |
| 200 | + Map<Integer, Integer> mp = new HashMap<>(); |
| 201 | + for (int num : arr) { |
| 202 | + int remainder = ((num % k) + k) % k; |
| 203 | + mp.put(remainder, mp.getOrDefault(remainder, 0) + 1); |
| 204 | + } |
| 205 | +
|
| 206 | + for (Map.Entry<Integer, Integer> entry : mp.entrySet()) { |
| 207 | + int remainder = entry.getKey(); |
| 208 | + int count = entry.getValue(); |
| 209 | + if (remainder == 0) { |
| 210 | + if (count % 2 != 0) return false; |
| 211 | + } else { |
| 212 | + int complement = k - remainder; |
| 213 | + if (!mp.get(remainder).equals(mp.get(complement))) { |
| 214 | + return false; |
| 215 | + } |
| 216 | + } |
| 217 | + } |
| 218 | + return true; |
| 219 | + } |
| 220 | +} |
| 221 | +
|
| 222 | + ``` |
| 223 | + |
| 224 | + </TabItem> |
| 225 | + <TabItem value="C++" label="C++"> |
| 226 | + <SolutionAuthor name="@hiteshgahanolia"/> |
| 227 | + ```cpp |
| 228 | + class Solution { |
| 229 | +public: |
| 230 | + bool canArrange(vector<int>& arr, int k) { |
| 231 | + map<int, int> mp; |
| 232 | + for (auto num : arr) { |
| 233 | + int remainder = ((num % k) + k) % k; |
| 234 | + mp[remainder]++; |
| 235 | + } |
| 236 | +
|
| 237 | + for (auto [remainder, count] : mp) { |
| 238 | + if (remainder == 0) { |
| 239 | + if (count % 2 != 0) return false; |
| 240 | + } else { |
| 241 | + int complement = k - remainder; |
| 242 | + if (mp[remainder] != mp[complement]) { |
| 243 | + return false; |
| 244 | + } |
| 245 | + } |
| 246 | + } |
| 247 | + return true; |
| 248 | + } |
| 249 | +}; |
| 250 | +``` |
| 251 | +</TabItem> |
| 252 | +</Tabs> |
| 253 | + |
| 254 | + </TabItem> |
| 255 | +</Tabs> |
| 256 | + |
| 257 | +## References |
| 258 | + |
| 259 | +- **LeetCode Problem**: [Check If Array Pairs Are Divisible by k](https://leetcode.com/problems/check-if-array-pairs-are-divisible-by-k/description/) |
| 260 | + |
| 261 | +- **Solution Link**: [LeetCode Solution](https://leetcode.com/problems/check-if-array-pairs-are-divisible-by-k/solutions) |
| 262 | + |
0 commit comments