> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Bitwise ORs of Subarrays Python Solution

> Tested Python solution for LeetCode 898 with 19 pytest cases. Generate a practice environment with lcpy.

LeetCode 898, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Dynamic Programming](/catalog/topics/dynamic-programming), [Bit Manipulation](/catalog/topics/bit-manipulation). [View on LeetCode](https://leetcode.com/problems/bitwise-ors-of-subarrays/description/).

Generate this problem as a practice environment: tested reference solution, 19 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 898   # by problem number
lcpy gen -s bitwise_ors_of_subarrays   # by problem name
```

## 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.

### Examples

```
Input: arr = [0]
Output: 1
Explanation: There is only one possible result: 0.
```

```
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.
```

```
Input: arr = [1,2,4]
Output: 6
Explanation: The possible results are 1, 2, 3, 4, 6, and 7.
```

### Constraints

* `1 <= arr.length <= 5 * 10^4`
* `0 <= arr[i] <= 10^9`

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/bitwise_ors_of_subarrays/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/bitwise_ors_of_subarrays/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n * 30) - each OR set only holds distinct prefix-OR values, at most 30 bits
    # Space: O(30 * n) worst case across the rolling and global sets
    def subarray_bitwise_ors(self, arr: list[int]) -> int:
        seen: set[int] = set()
        current: set[int] = set()
        for num in arr:
            current = {num | prev for prev in current} | {num}
            seen |= current
        return len(seen)
```

## Complexity

| Time | Space |
| - | - |
| O(n \* 30) - each OR set only holds distinct prefix-OR values, at most 30 bits | O(30 \* n) worst case across the rolling and global sets |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.