Skip to content

Commit 4e591b5

Browse files
committed
Add problem 3151: Special Array I
1 parent 2b9f0b8 commit 4e591b5

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2108,6 +2108,7 @@ pub mod problem_3143_maximum_points_inside_the_square;
21082108
pub mod problem_3146_permutation_difference_between_two_strings;
21092109
pub mod problem_3147_taking_maximum_energy_from_the_mystic_dungeon;
21102110
pub mod problem_3148_maximum_difference_score_in_a_grid;
2111+
pub mod problem_3151_special_array_i;
21112112
pub mod problem_3350_adjacent_increasing_subarrays_detection_ii;
21122113

21132114
#[cfg(test)]
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
pub struct Solution;
2+
3+
// ------------------------------------------------------ snip ------------------------------------------------------ //
4+
5+
impl Solution {
6+
pub fn is_array_special(nums: Vec<i32>) -> bool {
7+
let mut iter = nums.iter().copied();
8+
9+
iter.next().is_none_or(|mut prev| {
10+
iter.all(|num| {
11+
let result = (num ^ prev) & 1 != 0;
12+
13+
prev = num;
14+
15+
result
16+
})
17+
})
18+
}
19+
}
20+
21+
// ------------------------------------------------------ snip ------------------------------------------------------ //
22+
23+
impl super::Solution for Solution {
24+
fn is_array_special(nums: Vec<i32>) -> bool {
25+
Self::is_array_special(nums)
26+
}
27+
}
28+
29+
#[cfg(test)]
30+
mod tests {
31+
#[test]
32+
fn test_solution() {
33+
super::super::tests::run::<super::Solution>();
34+
}
35+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
pub mod iterative;
2+
3+
pub trait Solution {
4+
fn is_array_special(nums: Vec<i32>) -> bool;
5+
}
6+
7+
#[cfg(test)]
8+
mod tests {
9+
use super::Solution;
10+
11+
pub fn run<S: Solution>() {
12+
let test_cases = [(&[1] as &[_], true), (&[2, 1, 4], true), (&[4, 3, 1, 6], false)];
13+
14+
for (nums, expected) in test_cases {
15+
assert_eq!(S::is_array_special(nums.to_vec()), expected);
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)