Skip to content

Commit

Permalink
Add problem 2317: Maximum XOR After Operations
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Nov 12, 2024
1 parent 1db1efa commit 16d50ec
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
25 changes: 25 additions & 0 deletions src/problem_2317_maximum_xor_after_operations/mathematical.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
pub struct Solution;

// ------------------------------------------------------ snip ------------------------------------------------------ //

impl Solution {
pub fn maximum_xor(nums: Vec<i32>) -> i32 {
nums.iter().fold(0, |acc, &num| acc | num)
}
}

// ------------------------------------------------------ snip ------------------------------------------------------ //

impl super::Solution for Solution {
fn maximum_xor(nums: Vec<i32>) -> i32 {
Self::maximum_xor(nums)
}
}

#[cfg(test)]
mod tests {
#[test]
fn test_solution() {
super::super::tests::run::<super::Solution>();
}
}
18 changes: 18 additions & 0 deletions src/problem_2317_maximum_xor_after_operations/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
pub mod mathematical;

pub trait Solution {
fn maximum_xor(nums: Vec<i32>) -> i32;
}

#[cfg(test)]
mod tests {
use super::Solution;

pub fn run<S: Solution>() {
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);
}
}
}

0 comments on commit 16d50ec

Please sign in to comment.