diff --git a/src/lib.rs b/src/lib.rs index 610670b1..7dd17861 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1482,6 +1482,7 @@ pub mod problem_2190_most_frequent_number_following_key_in_an_array; pub mod problem_2194_cells_in_a_range_on_an_excel_sheet; pub mod problem_2206_divide_array_into_equal_pairs; pub mod problem_2215_find_the_difference_of_two_arrays; +pub mod problem_2220_minimum_bit_flips_to_convert_number; #[cfg(test)] mod test_utilities; diff --git a/src/problem_2220_minimum_bit_flips_to_convert_number/mod.rs b/src/problem_2220_minimum_bit_flips_to_convert_number/mod.rs new file mode 100644 index 00000000..077bfb3c --- /dev/null +++ b/src/problem_2220_minimum_bit_flips_to_convert_number/mod.rs @@ -0,0 +1,18 @@ +pub mod xor; + +pub trait Solution { + fn min_bit_flips(start: i32, goal: i32) -> i32; +} + +#[cfg(test)] +mod tests { + use super::Solution; + + pub fn run() { + let test_cases = [((10, 7), 3), ((3, 4), 3)]; + + for ((start, goal), expected) in test_cases { + assert_eq!(S::min_bit_flips(start, goal), expected); + } + } +} diff --git a/src/problem_2220_minimum_bit_flips_to_convert_number/xor.rs b/src/problem_2220_minimum_bit_flips_to_convert_number/xor.rs new file mode 100644 index 00000000..979d14c3 --- /dev/null +++ b/src/problem_2220_minimum_bit_flips_to_convert_number/xor.rs @@ -0,0 +1,25 @@ +pub struct Solution; + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl Solution { + pub fn min_bit_flips(start: i32, goal: i32) -> i32 { + (start ^ goal).count_ones() as _ + } +} + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl super::Solution for Solution { + fn min_bit_flips(start: i32, goal: i32) -> i32 { + Self::min_bit_flips(start, goal) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn test_solution() { + super::super::tests::run::(); + } +}