#2917
Easy Algorithms Find the k or of an array
Array Bit Manipulation
72.7% acceptance
Feb 25, 2026
250
278
You are given an integer array nums, and an integer k. Let's introduce K-or operation by extending the standard bitwise OR.
In K-or, a bit position in the result is set to 1 if at least k numbers in nums have a 1 in that position.
Return the K-or of nums.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn find_k_or(nums: Vec<i32>, k: i32) -> i32 {
let mut result = 0i32;
for bit in 0..31 {
let count = nums.iter().filter(|&&x| (x >> bit) & 1 == 1).count();
if count >= k as usize {
result |= 1 << bit;
}
}
result
}
}