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 arrayanswerof sizenwhereanswer[i]_is the length of theminimum sized subarray starting at _iwithmaximum bitwise OR.
A subarray is a contiguous non-empty sequence of elements within an array.
Input: nums =[1,0,2,1,3]Output: [3,3,2,2,1]Explanation:
The maximum possible bitwise OR starting at any index is3.- 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].
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].
To find the smallest subarray starting at each index with the maximum OR, we can process the array from right to left, tracking for each bit the rightmost position where it appears. The answer for each index is the farthest right position needed to cover all bits in the maximum OR.
classSolution {
public: vector<int> smallestSubarrays(vector<int>& nums) {
int n = nums.size();
vector<int> pos(32, -1), ans(n, 1);
for (int i = n-1; i >=0; --i) {
for (int b =0; b <32; ++b)
if (nums[i] & (1<<b)) pos[b] = i;
int far = i;
for (int b =0; b <32; ++b)
if (pos[b] !=-1) far = max(far, pos[b]);
ans[i] = far - i +1;
}
return ans;
}
};
classSolution {
publicint[]smallestSubarrays(int[] nums) {
int n = nums.length;
int[] pos =newint[32], ans =newint[n];
Arrays.fill(pos, -1);
for (int i = n-1; i >= 0; --i) {
for (int b = 0; b < 32; ++b)
if ((nums[i]& (1<<b)) != 0) pos[b]= i;
int far = i;
for (int b = 0; b < 32; ++b)
if (pos[b]!=-1 && pos[b]> far) far = pos[b];
ans[i]= far - i + 1;
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
classSolution {
funsmallestSubarrays(nums: IntArray): IntArray {
val n = nums.size
val pos = IntArray(32) { -1 }
val ans = IntArray(n)
for (i in n-1 downTo 0) {
for (b in0 until 32) if ((nums[i] shr b) and 1==1) pos[b] = i
var far = i
for (b in0 until 32) if (pos[b] != -1&& pos[b] > far) far = pos[b]
ans[i] = far - i + 1 }
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
classSolution:
defsmallestSubarrays(self, nums: list[int]) -> list[int]:
n = len(nums)
pos = [-1]*32 ans = [1]*n
for i in range(n-1, -1, -1):
for b in range(32):
if nums[i] & (1<<b): pos[b] = i
far = i
for b in range(32):
if pos[b] !=-1and pos[b] > far: far = pos[b]
ans[i] = far - i +1return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
impl Solution {
pubfnsmallest_subarrays(nums: Vec<i32>) -> Vec<i32> {
let n = nums.len();
letmut pos =vec![-1; 32];
letmut ans =vec![1; n];
for i in (0..n).rev() {
for b in0..32 {
if nums[i] & (1<<b) !=0 { pos[b] = i asi32; }
}
letmut far = i asi32;
for b in0..32 {
if pos[b] !=-1&& pos[b] > far { far = pos[b]; }
}
ans[i] = far - i asi32+1;
}
ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
classSolution {
smallestSubarrays(nums: number[]):number[] {
constn=nums.length;
constpos= Array(32).fill(-1);
constans= Array(n).fill(1);
for (leti=n-1; i>=0; --i) {
for (letb=0; b<32; ++b)
if ((nums[i] & (1<<b)) !==0) pos[b] =i;
letfar=i;
for (letb=0; b<32; ++b)
if (pos[b] !==-1&&pos[b] >far) far=pos[b];
ans[i] =far-i+1;
}
returnans;
}
}