---
# System prepended metadata

title: 2411. Smallest Subarrays With Maximum Bitwise OR
tags: [Leetcode, Bit Manipulation, Medium]

---

# 2411. Smallest Subarrays With Maximum Bitwise OR
###### tags: `Leetcode` `Medium` `Bit Manipulation`
Link: https://leetcode.com/problems/smallest-subarrays-with-maximum-bitwise-or/description/
## 思路 $O(31n)$ $O(31)$
从后往前遍历array 然后记录每个bit最近一次出现的位置
然后对于一个新的num来说我们要找到每一个之前出现过的bit出现位置最靠后的一个
就是subarray的结尾
## Code
```java=
class Solution {
    public int[] smallestSubarrays(int[] nums) {
        int[] last = new int[31];
        int n = nums.length;
        int[] res = new int[n];
        for(int i=n-1; i>=0; i--){
            res[i] = 1;
            for(int j=0; j<31; j++){
                if(((nums[i]>>j)&1)>0){
                    last[j] = i;
                }
                res[i] = Math.max(res[i], last[j]-i+1);
            }
        }
        return res;
    }
}
```