From 16d50ec08a2bf269e5a87661728d1b77283dfe4e Mon Sep 17 00:00:00 2001 From: EFanZh Date: Tue, 12 Nov 2024 21:58:44 +0800 Subject: [PATCH] Add problem 2317: Maximum XOR After Operations --- src/lib.rs | 1 + .../mathematical.rs | 25 +++++++++++++++++++ .../mod.rs | 18 +++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 src/problem_2317_maximum_xor_after_operations/mathematical.rs create mode 100644 src/problem_2317_maximum_xor_after_operations/mod.rs diff --git a/src/lib.rs b/src/lib.rs index 6843199b..28d24901 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1720,6 +1720,7 @@ pub mod problem_2309_greatest_english_letter_in_upper_and_lower_case; pub mod problem_2310_sum_of_numbers_with_units_digit_k; pub mod problem_2315_count_asterisks; pub mod problem_2316_count_unreachable_pairs_of_nodes_in_an_undirected_graph; +pub mod problem_2317_maximum_xor_after_operations; #[cfg(test)] mod test_utilities; diff --git a/src/problem_2317_maximum_xor_after_operations/mathematical.rs b/src/problem_2317_maximum_xor_after_operations/mathematical.rs new file mode 100644 index 00000000..4520341b --- /dev/null +++ b/src/problem_2317_maximum_xor_after_operations/mathematical.rs @@ -0,0 +1,25 @@ +pub struct Solution; + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl Solution { + pub fn maximum_xor(nums: Vec) -> i32 { + nums.iter().fold(0, |acc, &num| acc | num) + } +} + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl super::Solution for Solution { + fn maximum_xor(nums: Vec) -> i32 { + Self::maximum_xor(nums) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn test_solution() { + super::super::tests::run::(); + } +} diff --git a/src/problem_2317_maximum_xor_after_operations/mod.rs b/src/problem_2317_maximum_xor_after_operations/mod.rs new file mode 100644 index 00000000..e94c5773 --- /dev/null +++ b/src/problem_2317_maximum_xor_after_operations/mod.rs @@ -0,0 +1,18 @@ +pub mod mathematical; + +pub trait Solution { + fn maximum_xor(nums: Vec) -> i32; +} + +#[cfg(test)] +mod tests { + use super::Solution; + + pub fn run() { + let test_cases = [(&[3, 2, 4, 6] as &[_], 7), (&[1, 2, 3, 9, 2], 11)]; + + for (nums, expected) in test_cases { + assert_eq!(S::maximum_xor(nums.to_vec()), expected); + } + } +}