Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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.
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.
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].
Constraints:
n == nums.length1 <= n <= 1050 <= nums[i] <= 109Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Binary Search · Bit Manipulation · Sliding Window
[1,0,2,1,3]
[1,2]
merge-k-sorted-lists)bitwise-ors-of-subarrays)longest-subarray-with-maximum-bitwise-and)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2411: Smallest Subarrays With Maximum Bitwise OR
class Solution {
public int[] smallestSubarrays(int[] nums) {
int n = nums.length;
int[] ans = new int[n];
int[] f = new int[32];
Arrays.fill(f, -1);
for (int i = n - 1; i >= 0; --i) {
int t = 1;
for (int j = 0; j < 32; ++j) {
if (((nums[i] >> j) & 1) == 1) {
f[j] = i;
} else if (f[j] != -1) {
t = Math.max(t, f[j] - i + 1);
}
}
ans[i] = t;
}
return ans;
}
}
// Accepted solution for LeetCode #2411: Smallest Subarrays With Maximum Bitwise OR
func smallestSubarrays(nums []int) []int {
n := len(nums)
f := make([]int, 32)
for i := range f {
f[i] = -1
}
ans := make([]int, n)
for i := n - 1; i >= 0; i-- {
t := 1
for j := 0; j < 32; j++ {
if ((nums[i] >> j) & 1) == 1 {
f[j] = i
} else if f[j] != -1 {
t = max(t, f[j]-i+1)
}
}
ans[i] = t
}
return ans
}
# Accepted solution for LeetCode #2411: Smallest Subarrays With Maximum Bitwise OR
class Solution:
def smallestSubarrays(self, nums: List[int]) -> List[int]:
n = len(nums)
ans = [1] * n
f = [-1] * 32
for i in range(n - 1, -1, -1):
t = 1
for j in range(32):
if (nums[i] >> j) & 1:
f[j] = i
elif f[j] != -1:
t = max(t, f[j] - i + 1)
ans[i] = t
return ans
// Accepted solution for LeetCode #2411: Smallest Subarrays With Maximum Bitwise OR
impl Solution {
pub fn smallest_subarrays(nums: Vec<i32>) -> Vec<i32> {
let n = nums.len();
let mut ans = vec![1; n];
let mut f = vec![-1; 32];
for i in (0..n).rev() {
let mut t = 1;
for j in 0..32 {
if (nums[i] >> j) & 1 != 0 {
f[j] = i as i32;
} else if f[j] != -1 {
t = t.max(f[j] - i as i32 + 1);
}
}
ans[i] = t;
}
ans
}
}
// Accepted solution for LeetCode #2411: Smallest Subarrays With Maximum Bitwise OR
function smallestSubarrays(nums: number[]): number[] {
const n = nums.length;
const ans: number[] = Array(n).fill(1);
const f: number[] = Array(32).fill(-1);
for (let i = n - 1; i >= 0; i--) {
let t = 1;
for (let j = 0; j < 32; j++) {
if ((nums[i] >> j) & 1) {
f[j] = i;
} else if (f[j] !== -1) {
t = Math.max(t, f[j] - i + 1);
}
}
ans[i] = t;
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Check every element from left to right until we find the target or exhaust the array. Each comparison is O(1), and we may visit all n elements, giving O(n). No extra space needed.
Each comparison eliminates half the remaining search space. After k comparisons, the space is n/2ᵏ. We stop when the space is 1, so k = log₂ n. No extra memory needed — just two pointers (lo, hi).
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Setting `lo = mid` or `hi = mid` can stall and create an infinite loop.
Usually fails on: Two-element ranges never converge.
Fix: Use `lo = mid + 1` or `hi = mid - 1` where appropriate.
Wrong move: Using `if` instead of `while` leaves the window invalid for multiple iterations.
Usually fails on: Over-limit windows stay invalid and produce wrong lengths/counts.
Fix: Shrink in a `while` loop until the invariant is valid again.