Imagine you're a data analyst working with bit patterns, and you need to find the most powerful combination starting from each position in your dataset.
Given a 0-indexed array nums of length n containing non-negative integers, your task is to find the shortest subarray starting at each index that achieves the maximum possible bitwise OR.
Here's what you need to do:
- For each starting position
i, find all possible subarraysnums[i...j]wherej >= i - Calculate the bitwise OR of each subarray
- Find the maximum OR value achievable from position
i - Return the length of the shortest subarray that achieves this maximum
Key insight: The bitwise OR operation only adds bits (never removes them), so longer subarrays can only have greater or equal OR values. Your challenge is finding the shortest path to the maximum!
Example: If nums = [1, 0, 2, 1, 3], starting from index 0, the maximum OR we can achieve is 3 (from subarray [1,0,2,1,3]), but we might achieve it sooner with a shorter subarray.
Input & Output
Visualization
Time & Space Complexity
For each of n starting positions, we check up to n ending positions
Only using constant extra space for variables
Constraints
- 1 β€ nums.length β€ 105
- 0 β€ nums[i] β€ 109
- Each element fits in a 32-bit integer