From 59e921904069d027dccc09972fa050c7657c3e74 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Wed, 6 Mar 2024 09:58:10 +0800 Subject: [PATCH] 20240306 Finished --- src/problem/mod.rs | 3 +- .../p2917_find_the_k_or_of_an_array.rs | 43 +++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 src/problem/p2917_find_the_k_or_of_an_array.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 82db67d..47dfe5f 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -64,4 +64,5 @@ mod p2369_check_if_there_is_a_valid_partition_for_the_array; mod p2368_reachable_nodes_with_restrictions; mod p225_implement_stack_using_queues; mod p232_implement_queue_using_stacks; -mod p1976_number_of_ways_to_arrive_at_destination; \ No newline at end of file +mod p1976_number_of_ways_to_arrive_at_destination; +mod p2917_find_the_k_or_of_an_array; \ No newline at end of file diff --git a/src/problem/p2917_find_the_k_or_of_an_array.rs b/src/problem/p2917_find_the_k_or_of_an_array.rs new file mode 100644 index 0000000..b69ad1c --- /dev/null +++ b/src/problem/p2917_find_the_k_or_of_an_array.rs @@ -0,0 +1,43 @@ +/** + * [2917] Find the K-or of an Array + */ +pub struct Solution {} + + +// submission codes start here + +impl Solution { + pub fn find_k_or(nums: Vec, k: i32) -> i32 { + let mut result = 0; + + for i in 0..32 { + let mut count = 0; + + let x = 1 << i; + for num in &nums { + if *num & x == x { + count += 1; + } + } + + if count >= k { + dbg!(x); + result += x; + } + } + + result + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_2917() { + assert_eq!(9, Solution::find_k_or(vec![7,12,9,8,9,15], 4)); + } +}