2411. Smallest Subarrays With Maximum Bitwise OR Medium

@problem@discussion
#Array#Binary Search#Bit Manipulation#Sliding Window



1/**
2 * [2411] Smallest Subarrays With Maximum Bitwise OR
3 *
4 * 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.
5 * 
6 * 	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.
7 * 
8 * The bitwise OR of an array is the bitwise OR of all the numbers in it.
9 * 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.
10 * A subarray is a contiguous non-empty sequence of elements within an array.
11 *  
12 * Example 1:
13 * 
14 * Input: nums = [1,0,2,1,3]
15 * Output: [3,3,2,2,1]
16 * Explanation:
17 * The maximum possible bitwise OR starting at any index is 3. 
18 * - Starting at index 0, the shortest subarray that yields it is [1,0,2].
19 * - Starting at index 1, the shortest subarray that yields the maximum bitwise OR is [0,2,1].
20 * - Starting at index 2, the shortest subarray that yields the maximum bitwise OR is [2,1].
21 * - Starting at index 3, the shortest subarray that yields the maximum bitwise OR is [1,3].
22 * - Starting at index 4, the shortest subarray that yields the maximum bitwise OR is [3].
23 * Therefore, we return [3,3,2,2,1]. 
24 * 
25 * Example 2:
26 * 
27 * Input: nums = [1,2]
28 * Output: [2,1]
29 * Explanation:
30 * Starting at index 0, the shortest subarray that yields the maximum bitwise OR is of length 2.
31 * Starting at index 1, the shortest subarray that yields the maximum bitwise OR is of length 1.
32 * Therefore, we return [2,1].
33 * 
34 *  
35 * Constraints:
36 * 
37 * 	n == nums.length
38 * 	1 <= n <= 10^5
39 * 	0 <= nums[i] <= 10^9
40 * 
41 */
42pub struct Solution {}
43
44// problem: https://leetcode.com/problems/smallest-subarrays-with-maximum-bitwise-or/
45// discuss: https://leetcode.com/problems/smallest-subarrays-with-maximum-bitwise-or/discuss/?currentPage=1&orderBy=most_votes&query=
46
47// submission codes start here
48
49impl Solution {
50    pub fn smallest_subarrays(nums: Vec<i32>) -> Vec<i32> {
51        vec![]
52    }
53}
54
55// submission codes end
56
57#[cfg(test)]
58mod tests {
59    use super::*;
60
61    #[test]
62    fn test_2411() {
63    }
64}
65


Back
© 2025 bowen.ge All Rights Reserved.