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.
Given an integer array arr, return the number of distinct bitwise ORs of all the non-empty subarrays of arr.
The bitwise OR of a subarray is the bitwise OR of each integer in the subarray. The bitwise OR of a subarray of one integer is that integer.
A subarray is a contiguous non-empty sequence of elements within an array.
Example 1:
Input: arr = [0] Output: 1 Explanation: There is only one possible result: 0.
Example 2:
Input: arr = [1,1,2] Output: 3 Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2]. These yield the results 1, 1, 2, 1, 3, 3. There are 3 unique values, so the answer is 3.
Example 3:
Input: arr = [1,2,4] Output: 6 Explanation: The possible results are 1, 2, 3, 4, 6, and 7.
Constraints:
1 <= arr.length <= 5 * 1040 <= arr[i] <= 109Problem summary: Given an integer array arr, return the number of distinct bitwise ORs of all the non-empty subarrays of arr. The bitwise OR of a subarray is the bitwise OR of each integer in the subarray. The bitwise OR of a subarray of one integer is that integer. 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 · Dynamic Programming · Bit Manipulation
[0]
[1,1,2]
[1,2,4]
longest-nice-subarray)smallest-subarrays-with-maximum-bitwise-or)bitwise-or-of-all-subsequence-sums)find-the-maximum-sequence-value-of-array)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #898: Bitwise ORs of Subarrays
class Solution {
public int subarrayBitwiseORs(int[] arr) {
Set<Integer> ans = new HashSet<>();
Set<Integer> s = new HashSet<>();
for (int x : arr) {
Set<Integer> t = new HashSet<>();
for (int y : s) {
t.add(x | y);
}
t.add(x);
ans.addAll(t);
s = t;
}
return ans.size();
}
}
// Accepted solution for LeetCode #898: Bitwise ORs of Subarrays
func subarrayBitwiseORs(arr []int) int {
ans := map[int]bool{}
s := map[int]bool{}
for _, x := range arr {
t := map[int]bool{x: true}
for y := range s {
t[x|y] = true
}
for y := range t {
ans[y] = true
}
s = t
}
return len(ans)
}
# Accepted solution for LeetCode #898: Bitwise ORs of Subarrays
class Solution:
def subarrayBitwiseORs(self, arr: List[int]) -> int:
ans = set()
s = set()
for x in arr:
s = {x | y for y in s} | {x}
ans |= s
return len(ans)
// Accepted solution for LeetCode #898: Bitwise ORs of Subarrays
struct Solution;
use std::collections::HashSet;
impl Solution {
fn subarray_bitwise_o_rs(a: Vec<i32>) -> i32 {
let mut res: HashSet<i32> = HashSet::new();
let mut prev: HashSet<i32> = HashSet::new();
for x in a {
let mut cur: HashSet<i32> = HashSet::new();
cur.insert(x);
for y in prev {
cur.insert(y | x);
}
for &x in &cur {
res.insert(x);
}
prev = cur;
}
res.len() as i32
}
}
#[test]
fn test() {
let a = vec![0];
let res = 1;
assert_eq!(Solution::subarray_bitwise_o_rs(a), res);
let a = vec![1, 1, 2];
let res = 3;
assert_eq!(Solution::subarray_bitwise_o_rs(a), res);
let a = vec![1, 2, 4];
let res = 6;
assert_eq!(Solution::subarray_bitwise_o_rs(a), res);
}
// Accepted solution for LeetCode #898: Bitwise ORs of Subarrays
function subarrayBitwiseORs(arr: number[]): number {
const ans: Set<number> = new Set();
const s: Set<number> = new Set();
for (const x of arr) {
const t: Set<number> = new Set([x]);
for (const y of s) {
t.add(x | y);
}
s.clear();
for (const y of t) {
ans.add(y);
s.add(y);
}
}
return ans.size;
}
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
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: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.