Shortest Subarray With OR at Least K II - Problem
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.
Input & Output
Example 1 — Basic Case
$
Input:
nums = [1,2,3], k = 2
›
Output:
1
💡 Note:
The subarray [2] has OR = 2 ≥ 2, and [3] has OR = 3 ≥ 2. Both have length 1, which is minimum.
Example 2 — Need Multiple Elements
$
Input:
nums = [2,1,8], k = 10
›
Output:
3
💡 Note:
No single element ≥ 10. [2,1] gives OR = 3 < 10. Need all three: [2,1,8] gives OR = 2|1|8 = 11 ≥ 10.
Example 3 — No Solution
$
Input:
nums = [1,2], k = 5
›
Output:
-1
💡 Note:
Maximum possible OR is 1|2 = 3, which is less than k = 5. No special subarray exists.
Constraints
- 1 ≤ nums.length ≤ 2 × 105
- 0 ≤ nums[i] ≤ 109
- 0 ≤ k ≤ 109
Visualization
Tap to expand
Understanding the Visualization
1
Input
Array of non-negative integers and target k
2
Process
Check all subarrays, compute their bitwise OR
3
Output
Length of shortest subarray with OR ≥ k, or -1
Key Takeaway
🎯 Key Insight: Bitwise OR only increases when adding elements, making sliding window optimization possible with bit frequency tracking.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code