Skip to content

Commit 5a26397

Browse files
committed
feat: add solutions to lc problem: No.1497
1 parent 86225ea commit 5a26397

File tree

5 files changed

+118
-48
lines changed

5 files changed

+118
-48
lines changed

solution/1400-1499/1497.Check If Array Pairs Are Divisible by k/README.md

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ tags:
7474

7575
两个数 $a$ 和 $b$ 的和能被 $k$ 整除,当且仅当这两个数分别对 $k$ 取模的结果之和能被 $k$ 整除。
7676

77-
因此,我们可以统计数组中每个数对 $k$ 取模的结果,即余数,记录在数组 `cnt` 中。然后我们遍历数组 `cnt`,对于范围在 $[1,..k-1]$ 的每个数 $i$,如果 $cnt[i]$ 和 $cnt[k-i]$ 的值不相等,说明无法将数组中的数字分为 $n/2$ 对,使得每对数字的和都能被 $k$ 整除。如果 $cnt[0]$ 的值不是偶数,也说明无法将数组中的数字分为 $n/2$ 对,使得每对数字的和都能被 $k$ 整除。
77+
因此,我们可以统计数组中每个数对 $k$ 取模的结果,即余数,记录在数组 $\textit{cnt}$ 中。然后我们遍历数组 $\textit{cnt}$,对于范围在 $[1,..k-1]$ 的每个数 $i$,如果 $\textit{cnt}[i]$ 和 $\textit{cnt}[k-i]$ 的值不相等,说明无法将数组中的数字分为 $n/2$ 对,使得每对数字的和都能被 $k$ 整除。如果 $\textit{cnt}[0]$ 的值不是偶数,也说明无法将数组中的数字分为 $n/2$ 对,使得每对数字的和都能被 $k$ 整除。
7878

79-
时间复杂度 $O(n)$,空间复杂度 $O(k)$。其中 $n$ 为数组 `arr` 的长度。
79+
时间复杂度 $O(n)$,其中 $n$ 为数组 $\textit{arr}$ 的长度。空间复杂度 $O(k)$
8080

8181
<!-- tabs:start -->
8282

@@ -149,36 +149,59 @@ func canArrange(arr []int, k int) bool {
149149

150150
```ts
151151
function canArrange(arr: number[], k: number): boolean {
152-
const cnt = Array(k).fill(0);
153-
152+
const cnt: number[] = Array(k).fill(0);
154153
for (const x of arr) {
155-
cnt[((x % k) + k) % k]++;
154+
++cnt[((x % k) + k) % k];
156155
}
157-
158-
for (let i = 1; i < k; i++) {
159-
if (cnt[i] !== cnt[k - i]) return false;
156+
for (let i = 1; i < k; ++i) {
157+
if (cnt[i] !== cnt[k - i]) {
158+
return false;
159+
}
160160
}
161-
162161
return cnt[0] % 2 === 0;
163162
}
164163
```
165164

165+
#### Rust
166+
167+
```rust
168+
impl Solution {
169+
pub fn can_arrange(arr: Vec<i32>, k: i32) -> bool {
170+
let k = k as usize;
171+
let mut cnt = vec![0; k];
172+
for &x in &arr {
173+
cnt[((x % k as i32 + k as i32) % k as i32) as usize] += 1;
174+
}
175+
for i in 1..k {
176+
if cnt[i] != cnt[k - i] {
177+
return false;
178+
}
179+
}
180+
cnt[0] % 2 == 0
181+
}
182+
}
183+
```
184+
166185
#### JavaScript
167186

168187
```js
169-
function canArrange(arr, k) {
188+
/**
189+
* @param {number[]} arr
190+
* @param {number} k
191+
* @return {boolean}
192+
*/
193+
var canArrange = function (arr, k) {
170194
const cnt = Array(k).fill(0);
171-
172195
for (const x of arr) {
173-
cnt[((x % k) + k) % k]++;
196+
++cnt[((x % k) + k) % k];
174197
}
175-
176-
for (let i = 1; i < k; i++) {
177-
if (cnt[i] !== cnt[k - i]) return false;
198+
for (let i = 1; i < k; ++i) {
199+
if (cnt[i] !== cnt[k - i]) {
200+
return false;
201+
}
178202
}
179-
180203
return cnt[0] % 2 === 0;
181-
}
204+
};
182205
```
183206

184207
<!-- tabs:end -->

solution/1400-1499/1497.Check If Array Pairs Are Divisible by k/README_EN.md

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,13 @@ tags:
6868

6969
<!-- solution:start -->
7070

71-
### Solution 1
71+
### Solution 1: Counting Remainders
72+
73+
The sum of two numbers $a$ and $b$ is divisible by $k$ if and only if the sum of their remainders when divided by $k$ is divisible by $k$.
74+
75+
Therefore, we can count the remainder of each number in the array when divided by $k$, and record them in an array $\textit{cnt}$. Then we traverse the array $\textit{cnt}$. For each number $i$ in the range $[1,..k-1]$, if the values of $\textit{cnt}[i]$ and $\textit{cnt}[k-i]$ are not equal, it means we cannot divide the numbers in the array into $n/2$ pairs such that the sum of each pair is divisible by $k$. Similarly, if the value of $\textit{cnt}[0]$ is not even, it also means we cannot divide the numbers in the array into $n/2$ pairs such that the sum of each pair is divisible by $k$.
76+
77+
The time complexity is $O(n)$, where $n$ is the length of the array $\textit{arr}$. The space complexity is $O(k)$.
7278

7379
<!-- tabs:start -->
7480

@@ -141,36 +147,59 @@ func canArrange(arr []int, k int) bool {
141147

142148
```ts
143149
function canArrange(arr: number[], k: number): boolean {
144-
const cnt = Array(k).fill(0);
145-
150+
const cnt: number[] = Array(k).fill(0);
146151
for (const x of arr) {
147-
cnt[((x % k) + k) % k]++;
152+
++cnt[((x % k) + k) % k];
148153
}
149-
150-
for (let i = 1; i < k; i++) {
151-
if (cnt[i] !== cnt[k - i]) return false;
154+
for (let i = 1; i < k; ++i) {
155+
if (cnt[i] !== cnt[k - i]) {
156+
return false;
157+
}
152158
}
153-
154159
return cnt[0] % 2 === 0;
155160
}
156161
```
157162

163+
#### Rust
164+
165+
```rust
166+
impl Solution {
167+
pub fn can_arrange(arr: Vec<i32>, k: i32) -> bool {
168+
let k = k as usize;
169+
let mut cnt = vec![0; k];
170+
for &x in &arr {
171+
cnt[((x % k as i32 + k as i32) % k as i32) as usize] += 1;
172+
}
173+
for i in 1..k {
174+
if cnt[i] != cnt[k - i] {
175+
return false;
176+
}
177+
}
178+
cnt[0] % 2 == 0
179+
}
180+
}
181+
```
182+
158183
#### JavaScript
159184

160185
```js
161-
function canArrange(arr, k) {
186+
/**
187+
* @param {number[]} arr
188+
* @param {number} k
189+
* @return {boolean}
190+
*/
191+
var canArrange = function (arr, k) {
162192
const cnt = Array(k).fill(0);
163-
164193
for (const x of arr) {
165-
cnt[((x % k) + k) % k]++;
194+
++cnt[((x % k) + k) % k];
166195
}
167-
168-
for (let i = 1; i < k; i++) {
169-
if (cnt[i] !== cnt[k - i]) return false;
196+
for (let i = 1; i < k; ++i) {
197+
if (cnt[i] !== cnt[k - i]) {
198+
return false;
199+
}
170200
}
171-
172201
return cnt[0] % 2 === 0;
173-
}
202+
};
174203
```
175204

176205
<!-- tabs:end -->
Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
function canArrange(arr, k) {
1+
/**
2+
* @param {number[]} arr
3+
* @param {number} k
4+
* @return {boolean}
5+
*/
6+
var canArrange = function (arr, k) {
27
const cnt = Array(k).fill(0);
3-
48
for (const x of arr) {
5-
cnt[((x % k) + k) % k]++;
9+
++cnt[((x % k) + k) % k];
610
}
7-
8-
for (let i = 1; i < k; i++) {
9-
if (cnt[i] !== cnt[k - i]) return false;
11+
for (let i = 1; i < k; ++i) {
12+
if (cnt[i] !== cnt[k - i]) {
13+
return false;
14+
}
1015
}
11-
1216
return cnt[0] % 2 === 0;
13-
}
17+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
impl Solution {
2+
pub fn can_arrange(arr: Vec<i32>, k: i32) -> bool {
3+
let k = k as usize;
4+
let mut cnt = vec![0; k];
5+
for &x in &arr {
6+
cnt[((x % k as i32 + k as i32) % k as i32) as usize] += 1;
7+
}
8+
for i in 1..k {
9+
if cnt[i] != cnt[k - i] {
10+
return false;
11+
}
12+
}
13+
cnt[0] % 2 == 0
14+
}
15+
}
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
function canArrange(arr: number[], k: number): boolean {
2-
const cnt = Array(k).fill(0);
3-
2+
const cnt: number[] = Array(k).fill(0);
43
for (const x of arr) {
5-
cnt[((x % k) + k) % k]++;
4+
++cnt[((x % k) + k) % k];
65
}
7-
8-
for (let i = 1; i < k; i++) {
9-
if (cnt[i] !== cnt[k - i]) return false;
6+
for (let i = 1; i < k; ++i) {
7+
if (cnt[i] !== cnt[k - i]) {
8+
return false;
9+
}
1010
}
11-
1211
return cnt[0] % 2 === 0;
1312
}

0 commit comments

Comments
 (0)