Smallest Subarrays With Maximum Bitwise OR - Problem

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 subarrays nums[i...j] where j >= 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

example_1.py β€” Basic Example
$ Input: nums = [1, 0, 2, 1, 3]
β€Ί Output: [5, 4, 3, 2, 1]
πŸ’‘ Note: From index 0: maximum OR is 3 (achieved by [1,0,2,1,3]), shortest length is 5. From index 1: maximum OR is 3 (achieved by [0,2,1,3]), shortest length is 4. And so on.
example_2.py β€” Decreasing Array
$ Input: nums = [8, 4, 2, 1]
β€Ί Output: [1, 1, 1, 1]
πŸ’‘ Note: Each element by itself gives the maximum OR from its position since the array is decreasing and OR cannot exceed the first element in any subarray.
example_3.py β€” Single Element
$ Input: nums = [5]
β€Ί Output: [1]
πŸ’‘ Note: With only one element, the shortest (and only) subarray has length 1.

Visualization

Tap to expand
Song 1🎡🎸Song 2πŸ₯Song 3🎺🎡Song 4🎀🎡 Ultimate Playlist BuilderEach song adds unique audio features (bits)Shortest playlist for maximum richnessAudio Features Tracker:🎡 Melody: Last at Song 3🎸 Guitar: Last at Song 1πŸ₯ Drums: Last at Song 2🎺 Brass: Last at Song 3🎀 Vocals: Last at Song 4➜ Must include up to Song 4!
Understanding the Visualization
1
Scan your music library
Look at all songs from current position to the end
2
Track audio features
Remember which songs have bass, treble, vocals, etc. (different bits)
3
Find essential songs
Identify the last song that contributes a unique audio feature
4
Create shortest playlist
Your playlist must reach that essential song to get maximum richness
Key Takeaway
🎯 Key Insight: By processing from right to left and tracking the rightmost position of each bit, we can efficiently determine the minimum playlist length needed to capture all unique audio features (maximum OR) from any starting song.

Time & Space Complexity

Time Complexity
⏱️
O(nΒ²)

For each of n starting positions, we check up to n ending positions

n
2n
⚠ Quadratic Growth
Space Complexity
O(1)

Only using constant extra space for variables

n
2n
βœ“ Linear Space

Constraints

  • 1 ≀ nums.length ≀ 105
  • 0 ≀ nums[i] ≀ 109
  • Each element fits in a 32-bit integer
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
28.0K Views
Medium Frequency
~25 min Avg. Time
850 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