diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 22d32f6..7f3fd54 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -426,3 +426,5 @@ mod p3297_count_substrings_that_can_be_rearranged_to_contain_a_string_i; mod p3298_count_substrings_that_can_be_rearranged_to_contain_a_string_ii; mod p3270_find_the_key_of_the_numbers; + +mod p2275_largest_combination_with_bitwise_and_greater_than_zero; diff --git a/src/problem/p2275_largest_combination_with_bitwise_and_greater_than_zero.rs b/src/problem/p2275_largest_combination_with_bitwise_and_greater_than_zero.rs new file mode 100644 index 0000000..8bb00da --- /dev/null +++ b/src/problem/p2275_largest_combination_with_bitwise_and_greater_than_zero.rs @@ -0,0 +1,39 @@ +/** + * [2275] Largest Combination With Bitwise AND Greater Than Zero + */ +pub struct Solution {} + +// submission codes start here + +impl Solution { + pub fn largest_combination(candidates: Vec) -> i32 { + let mut result = 0; + + for i in 0..31 { + result = result.max( + candidates + .iter() + .filter_map(|&x| if x & (1 << i) > 0 { Some(()) } else { None }) + .count(), + ); + } + + result as i32 + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_2275() { + assert_eq!( + 4, + Solution::largest_combination(vec![16, 17, 71, 62, 12, 24, 14]) + ); + assert_eq!(2, Solution::largest_combination(vec![8, 8])); + } +}