#3095
Easy Algorithms Shortest subarray with or at least k i
Array Bit Manipulation Sliding Window
44.2% acceptance
Feb 25, 2026
137
21
You are given an array nums of non-negative integers and an integer k.
An array is called special if the bitwise OR of all of its elements is at least k.
Return the length of the shortest special non-empty subarray of nums, or return -1 if no special subarray exists.
Solution
Rust
Time O(n²)
Space O(1)
impl Solution {
pub fn minimum_subarray_length(nums: Vec<i32>, k: i32) -> i32 {
let n = nums.len();
let mut ans = i32::MAX;
for i in 0..n {
let mut or = 0i32;
for j in i..n {
or |= nums[j];
if or >= k { ans = ans.min((j - i + 1) as i32); break; }
}
}
if ans == i32::MAX { -1 } else { ans }
}
}