Smallest Subarrays With Maximum Bitwise OR - Problem

You are given a 0-indexed array nums of length n, consisting of non-negative integers. For each index i from 0 to n - 1, you must determine the size of the minimum sized non-empty subarray of nums starting at i (inclusive) that has the maximum possible bitwise OR.

In other words, let Bij be the bitwise OR of the subarray nums[i...j]. You need to find the smallest subarray starting at i, such that bitwise OR of this subarray is equal to max(Bik) where i ≤ k ≤ n - 1.

The bitwise OR of an array is the bitwise OR of all the numbers in it.

Return an integer array answer of size n where answer[i] is the length of the minimum sized subarray starting at i with maximum bitwise OR.

A subarray is a contiguous non-empty sequence of elements within an array.

Input & Output

Example 1 — Basic Case
$ Input: nums = [1,0,3,5]
Output: [4,3,2,1]
💡 Note: Starting at index 0: maximum OR is 1|0|3|5=7, minimum length is 4 (need [1,0,3,5]). Starting at index 1: maximum OR is 0|3|5=7, minimum length is 3 (need [0,3,5]). Starting at index 2: maximum OR is 3|5=7, minimum length is 2 (need [3,5]). Starting at index 3: maximum OR is 5, minimum length is 1 (need [5]).
Example 2 — Single Element
$ Input: nums = [8]
Output: [1]
💡 Note: Only one element, so the minimum subarray starting at index 0 has length 1 with OR=8.
Example 3 — All Same Values
$ Input: nums = [2,2,2]
Output: [1,1,1]
💡 Note: All elements are the same, so from each position, a single element achieves the maximum OR of 2.

Constraints

  • 1 ≤ nums.length ≤ 105
  • 0 ≤ nums[i] ≤ 108

Visualization

Tap to expand
Smallest Subarrays With Maximum Bitwise OR INPUT nums = [1, 0, 3, 5] 1 i=0 0 i=1 3 i=2 5 i=3 Binary Values 1 = 001 0 = 000 3 = 011 5 = 101 Goal Find smallest subarray starting at each i with maximum possible OR Track last position of each bit ALGORITHM STEPS 1 Initialize Track bit positions array last[32] for each bit 2 Traverse Right to Left For i = n-1 down to 0 Process each index 3 Update Bit Positions For each set bit in nums[i] Update last[bit] = i 4 Find Max Position farthest = max(last[b]) answer[i] = farthest - i + 1 Example: i=0 nums[0]=1, bits: bit0 Max OR=7 at j=3 answer[0]=3-0+1=3 FINAL RESULT Output: [3, 3, 2, 1] 3 i=0 3 i=1 2 i=2 1 i=3 Breakdown i=0: OR([1,0,3])=3, len=3 1|0|3 = 001|000|011 = 011 i=1: OR([0,3,5])=7, len=3 0|3|5 = 000|011|101 = 111 i=2: OR([3,5])=7, len=2 3|5 = 011|101 = 111 i=3: OR([5])=5, len=1 5 = 101 OK - Verified! Key Insight: By tracking the last (rightmost) position where each bit is set, we can efficiently find the minimum subarray length. The farthest position among all bits determines when we achieve maximum OR. Single pass O(n) with O(32) space for bit tracking. TutorialsPoint - Smallest Subarrays With Maximum Bitwise OR | Single Pass with Bit Tracking
Asked in
Microsoft 35 Google 28 Amazon 22
28.4K Views
Medium Frequency
~25 min Avg. Time
856 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