You're tasked with finding the shortest contiguous subarray within an array of non-negative integers where the bitwise OR of all elements equals or exceeds a given threshold k.
A subarray is considered "special" if when you perform the bitwise OR operation on all its elements, the result is at least k. The bitwise OR operation combines bits using the rule: if either bit is 1, the result is 1.
Your goal: Return the length of the shortest special non-empty subarray, or -1 if no such subarray exists.
Example: For array [2, 1, 8] and k = 10, the subarray [2, 8] has OR value 2 | 8 = 10, which meets our threshold with length 2.
Input & Output
Visualization
Time & Space Complexity
Each element visited twice, but OR recalculation for shrinking adds log factor
Only using pointer variables and OR accumulator
Constraints
- 1 โค nums.length โค 50
- 0 โค nums[i] โค 50
- 0 โค k โค 50
- All array elements are non-negative integers