#2411
Smallest Subarrays With Maximum Bitwise OR
specialist · 685 · lc medium +30 · verified · 62% accepted · 1,044 likes · top 63%
Description
Given a 0-indexed array nums of length n, produce a result array ans of the same length where ans[i] is the length of the shortest subarray starting at index i whose bitwise OR equals the maximum possible bitwise OR of any subarray starting at i.
Return ans.
Example 1:
Input: nums = [1,0,2,1,3]
Output: [3,3,2,2,1]
Explanation:
The maximum possible bitwise OR starting at any index is 3.
- Starting at index 0, the shortest subarray that yields it is [1,0,2].
- Starting at index 1, the shortest subarray that yields the maximum bitwise OR is [0,2,1].
- Starting at index 2, the shortest subarray that yields the maximum bitwise OR is [2,1].
- Starting at index 3, the shortest subarray that yields the maximum bitwise OR is [1,3].
- Starting at index 4, the shortest subarray that yields the maximum bitwise OR is [3].
Therefore, we return [3,3,2,2,1].
Example 2:
Input: nums = [1,2]
Output: [2,1]
Explanation:
Starting at index 0, the shortest subarray that yields the maximum bitwise OR is of length 2.
Starting at index 1, the shortest subarray that yields the maximum bitwise OR is of length 1.
Therefore, we return [2,1].
Code
1
2
3