Bitwise ORs of Subarrays - Problem
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.
Input & Output
Example 1 — Basic Case
$
Input:
arr = [0]
›
Output:
1
💡 Note:
Only one subarray [0], with bitwise OR = 0. So there is 1 distinct value.
Example 2 — Multiple Elements
$
Input:
arr = [1,1,2]
›
Output:
3
💡 Note:
Subarrays: [1]=1, [1]=1, [2]=2, [1,1]=1, [1,2]=3, [1,1,2]=3. Distinct ORs: {1,2,3}.
Example 3 — All Same Elements
$
Input:
arr = [1,2,4]
›
Output:
6
💡 Note:
Subarrays: [1]=1, [2]=2, [4]=4, [1,2]=3, [2,4]=6, [1,2,4]=7. Distinct ORs: {1,2,3,4,6,7}.
Constraints
- 1 ≤ arr.length ≤ 5 × 104
- 0 ≤ arr[i] ≤ 109
Visualization
Tap to expand
Understanding the Visualization
1
Input Array
Given array of integers
2
Generate Subarrays
Find all contiguous non-empty subarrays
3
Count Distinct ORs
Calculate OR for each subarray and count unique values
Key Takeaway
🎯 Key Insight: Bitwise OR can only add bits, so the number of distinct OR values is limited to at most 32 per position
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code