Shortest Subarray With OR at Least K II - Problem

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

example_1.py โ€” Basic Case
$ Input: nums = [1,2,3], k = 2
โ€บ Output: 1
๐Ÿ’ก Note: The subarray [2] has bitwise OR of 2, which equals k. This is the shortest possible with length 1.
example_2.py โ€” Multiple Elements Needed
$ Input: nums = [2,1,8], k = 10
โ€บ Output: 3
๐Ÿ’ก Note: We need all three elements: 2|1|8 = 11 โ‰ฅ 10. No shorter subarray achieves OR โ‰ฅ 10.
example_3.py โ€” Impossible Case
$ Input: nums = [1,2], k = 5
โ€บ Output: -1
๐Ÿ’ก Note: The maximum possible OR is 1|2 = 3, which is less than k = 5. No subarray can achieve the required OR.

Visualization

Tap to expand
Sliding Window with Bit CountingArray: [1, 2, 3, 7] (checking length 3)1237Window 1Bit Analysis for Window [1, 2, 3]:1 = 001โ‚‚, 2 = 010โ‚‚, 3 = 011โ‚‚Bit Counts:Bit0Count: 2 (from 1,3)Bit1Count: 2 (from 2,3)Bit2Count: 0OR = 011โ‚‚ = 3Slide window right:1237Window 2New Bit Analysis for Window [2, 3, 7]:2 = 010โ‚‚, 3 = 011โ‚‚, 7 = 111โ‚‚Updated Bit Counts:Bit0Count: 2 (from 3,7)Bit1Count: 2 (from 2,3)Bit2Count: 1 (from 7)OR = 111โ‚‚ = 7 โ‰ฅ k! โœ“๐ŸŽฏ Key: Efficiently maintain bit counts as we slide the window
Understanding the Visualization
1
Initialize bit counts
Count how many numbers have each bit set in the current window
2
Calculate current OR
If any bit has count > 0, that bit is set in the OR result
3
Slide the window
Add new element's bits, remove old element's bits, update OR accordingly
4
Check condition
If current OR โ‰ฅ k, this window length works
Key Takeaway
๐ŸŽฏ Key Insight: Binary search on length + sliding window with bit counting gives us O(n log n) time complexity. The bit counting technique allows us to efficiently maintain the OR value as we slide the window, avoiding recalculation.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n log n)

Binary search (log n) with sliding window check (O(n)) for each candidate length

n
2n
โšก Linearithmic
Space Complexity
O(1)

Only using arrays to track bit counts (constant size for 32 bits)

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค nums.length โ‰ค 2 ร— 105
  • 0 โ‰ค nums[i] โ‰ค 109
  • 0 โ‰ค k โ‰ค 109
  • All elements are non-negative integers
Asked in
Google 45 Amazon 38 Meta 28 Microsoft 22
52.0K Views
Medium-High Frequency
~25 min Avg. Time
1.3K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen