Skip to content

Commit ca54d12

Browse files
committed
LC 1497. Check If Array Pairs Are Divisible by k (Rust)
1 parent ad10613 commit ca54d12

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,7 @@ to the solution in this repository.
552552
| [1482. Minimum Number of Days to Make m Bouquets][lc1482] | 🟠 Medium | [![rust](res/rs.png)][lc1482rs] |
553553
| [1491. Average Salary Excluding the Minimum and Maximum Salary][lc1491] | 🟢 Easy | [![rust](res/rs.png)][lc1491rs] |
554554
| [1493. Longest Subarray of 1's After Deleting One Element][lc1493] | 🟠 Medium | [![rust](res/rs.png)][lc1493rs] |
555+
| [1497. Check If Array Pairs Are Divisible by k][lc1497] | 🟠 Medium | [![rust](res/rs.png)][lc1497rs] |
555556
| [1498. Number of Subsequences That Satisfy the Given Sum Condition][lc1498] | 🟠 Medium | [![python](res/py.png)][lc1498py] |
556557
| [1502. Can Make Arithmetic Progression From Sequence][lc1502] | 🟢 Easy | [![rust](res/rs.png)][lc1502rs] |
557558
| [1503. Last Moment Before All Ants Fall Out of a Plank][lc1503] | 🟠 Medium | [![rust](res/rs.png)][lc1503rs] |
@@ -1987,6 +1988,8 @@ to the solution in this repository.
19871988
[lc1491rs]: leetcode/average-salary-excluding-the-minimum-and-maximum-salary.rs
19881989
[lc1493]: https://leetcode.com/problems/longest-subarray-of-1s-after-deleting-one-element/
19891990
[lc1493rs]: leetcode/longest-subarray-of-1s-after-deleting-one-element.rs
1991+
[lc1497]: https://leetcode.com/problems/check-if-array-pairs-are-divisible-by-k/
1992+
[lc1497rs]: leetcode/check-if-array-pairs-are-divisible-by-k.rs
19901993
[lc1498]: https://leetcode.com/problems/number-of-subsequences-that-satisfy-the-given-sum-condition/
19911994
[lc1498py]: leetcode/number-of-subsequences-that-satisfy-the-given-sum-condition.py
19921995
[lc1502]: https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence/
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// 1497. Check If Array Pairs Are Divisible by k
2+
// 🟠 Medium
3+
//
4+
// https://leetcode.com/problems/check-if-array-pairs-are-divisible-by-k/
5+
//
6+
// Tags: Array - Hash Table - Counting
7+
8+
struct Solution;
9+
impl Solution {
10+
/// Solution overview
11+
///
12+
/// Time complexity: O() -
13+
/// Space complexity: O() -
14+
///
15+
/// Runtime ms Beats %
16+
/// Memory MB Beats %
17+
pub fn can_arrange(arr: Vec<i32>, k: i32) -> bool {
18+
let mut mods = vec![0; k as usize];
19+
for &num in &arr {
20+
mods[(((num % k) + k) % k) as usize] += 1;
21+
}
22+
if mods[0] % 2 != 0 {
23+
return false;
24+
}
25+
let k = k as usize;
26+
for i in 1..k / 2 + 1 {
27+
if mods[i] != mods[k - i] {
28+
return false;
29+
}
30+
}
31+
true
32+
}
33+
}
34+
35+
// Tests.
36+
fn main() {
37+
let tests = [
38+
(vec![-1, -1, -1, -1, 2, 2, -2, -2], 3, false),
39+
(vec![-1, 1, -2, 2, -3, 3, -4, 4], 3, true),
40+
(vec![1, 2, 3, 4, 5, 10, 6, 7, 8, 9], 5, true),
41+
(vec![1, 2, 3, 4, 5, 6], 7, true),
42+
(vec![1, 2, 3, 4, 5, 6], 10, false),
43+
];
44+
println!("\n\x1b[92m» Running {} tests...\x1b[0m", tests.len());
45+
let mut success = 0;
46+
for (i, t) in tests.iter().enumerate() {
47+
let res = Solution::can_arrange(t.0.clone(), t.1);
48+
if res == t.2 {
49+
success += 1;
50+
println!("\x1b[92m✔\x1b[95m Test {} passed!\x1b[0m", i);
51+
} else {
52+
println!(
53+
"\x1b[31mx\x1b[95m Test {} failed expected: {:?} but got {}!!\x1b[0m",
54+
i, t.2, res
55+
);
56+
}
57+
}
58+
println!();
59+
if success == tests.len() {
60+
println!("\x1b[30;42m✔ All tests passed!\x1b[0m")
61+
} else if success == 0 {
62+
println!("\x1b[31mx \x1b[41;37mAll tests failed!\x1b[0m")
63+
} else {
64+
println!(
65+
"\x1b[31mx\x1b[95m {} tests failed!\x1b[0m",
66+
tests.len() - success
67+
)
68+
}
69+
}

0 commit comments

Comments
 (0)