Skip to content

Commit feccd9d

Browse files
committed
Add problem 2980: Check if Bitwise OR Has Trailing Zeros
1 parent 64366a2 commit feccd9d

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2036,6 +2036,7 @@ pub mod problem_2966_divide_array_into_arrays_with_max_difference;
20362036
pub mod problem_2970_count_the_number_of_incremovable_subarrays_i;
20372037
pub mod problem_2972_count_the_number_of_incremovable_subarrays_ii;
20382038
pub mod problem_2974_minimum_number_game;
2039+
pub mod problem_2980_check_if_bitwise_or_has_trailing_zeros;
20392040

20402041
#[cfg(test)]
20412042
mod test_utilities;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
pub struct Solution;
2+
3+
// ------------------------------------------------------ snip ------------------------------------------------------ //
4+
5+
impl Solution {
6+
pub fn has_trailing_zeros(nums: Vec<i32>) -> bool {
7+
let mut iter = nums.iter().filter(|&num| num & 1 == 0);
8+
let mut found_one = || iter.next().is_some();
9+
10+
found_one() && found_one()
11+
}
12+
}
13+
14+
// ------------------------------------------------------ snip ------------------------------------------------------ //
15+
16+
impl super::Solution for Solution {
17+
fn has_trailing_zeros(nums: Vec<i32>) -> bool {
18+
Self::has_trailing_zeros(nums)
19+
}
20+
}
21+
22+
#[cfg(test)]
23+
mod tests {
24+
#[test]
25+
fn test_solution() {
26+
super::super::tests::run::<super::Solution>();
27+
}
28+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
pub mod greedy;
2+
3+
pub trait Solution {
4+
fn has_trailing_zeros(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 = [
13+
(&[1, 2, 3, 4, 5] as &[_], true),
14+
(&[2, 4, 8, 16], true),
15+
(&[1, 3, 5, 7, 9], false),
16+
];
17+
18+
for (nums, expected) in test_cases {
19+
assert_eq!(S::has_trailing_zeros(nums.to_vec()), expected);
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)