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

# Count Number of Maximum Bitwise-OR Subsets

> Tested Python solution for LeetCode 2044 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 2044, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Backtracking](/catalog/topics/backtracking), [Bit Manipulation](/catalog/topics/bit-manipulation), [Enumeration](/catalog/topics/enumeration). [View on LeetCode](https://leetcode.com/problems/count-number-of-maximum-bitwise-or-subsets/description/).

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

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

## Problem

Given an integer array `nums`, find the **maximum** possible **bitwise OR** of a subset of `nums` and return *the **number of different non-empty subsets** with the maximum bitwise OR*.

An array `a` is a **subset** of an array `b` if `a` can be obtained from `b` by deleting some (possibly zero) elements of `b`. Two subsets are considered **different** if the indices of the elements chosen are different.

The bitwise OR of an array `a` is equal to `a[0] OR a[1] OR ... OR a[a.length - 1]` (**0-indexed**).

### Examples

```
Input: nums = [3,1]
Output: 2
```

**Explanation:** The maximum possible bitwise OR of a subset is 3. There are 2 subsets with a bitwise OR of 3:

* `[3]`
* `[3,1]`

```
Input: nums = [2,2,2]
Output: 7
```

**Explanation:** All non-empty subsets of `[2,2,2]` have a bitwise OR of 2. There are 2^3 - 1 = 7 total subsets.

```
Input: nums = [3,2,1,5]
Output: 6
```

**Explanation:** The maximum possible bitwise OR of a subset is 7. There are 6 subsets with a bitwise OR of 7:

* `[3,5]`
* `[3,1,5]`
* `[3,2,5]`
* `[3,2,1,5]`
* `[2,5]`
* `[2,1,5]`

### Constraints

* `1 <= nums.length <= 16`
* `1 <= nums[i] <= 10^5`

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n * max_or) where max_or <= 2^17 for nums[i] <= 10^5
    # Space: O(max_or)
    def count_max_or_subsets(self, nums: list[int]) -> int:
        target = 0
        for num in nums:
            target |= num
        # counts[acc] = number of subsets (possibly empty) with OR value acc
        counts = [1] + [0] * target
        for num in nums:
            for acc in range(target, -1, -1):
                if counts[acc]:
                    counts[acc | num] += counts[acc]
        return counts[target]
```

## Complexity

| Time | Space |
| - | - |
| O(n \* max\_or) where max\_or \<= 2^17 for nums\[i] \<= 10^5 | O(max\_or) |

## Tags

[NeetCode All](/catalog/neetcode).


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