You are given an array nums of non-negative integers and an integer k. Your goal is to find the shortest contiguous subarray whose bitwise OR is at least k.
A subarray is called special if the bitwise OR of all its elements is greater than or equal to k. The bitwise OR operation combines bits from multiple numbers - if any number has a 1 in a particular bit position, the result will have a 1 in that position.
Return the length of the shortest special non-empty subarray of nums, or return -1 if no special subarray exists.
Example: For nums = [1, 2, 3] and k = 2, the subarray [2] has OR value 2 โฅ 2, so the answer is 1.
Input & Output
Visualization
Time & Space Complexity
Binary search (log n) with sliding window check (O(n)) for each candidate length
Only using arrays to track bit counts (constant size for 32 bits)
Constraints
- 1 โค nums.length โค 2 ร 105
- 0 โค nums[i] โค 109
- 0 โค k โค 109
- All elements are non-negative integers