Count Number of Maximum Bitwise-OR Subsets (LeetCode 2044): Solution and Explanation

Medium
Array
Backtracking
Bit Manipulation
Enumeration

Find the largest bitwise OR any non-empty subset can reach, then count how many subsets (by index) reach exactly that value.

Intuition

OR never clears a bit. Adding one more element to a subset can only keep its OR the same or make it larger, so the whole array always has the largest OR. That value is the target, and it costs one pass to compute.

What is left is a counting question: how many subsets have OR equal to the target? With at most 16 elements there are at most 65,536 subsets, so walking every take-or-skip decision is affordable.

There is a useful shortcut. Once the OR of the chosen elements already equals the target, nothing added later can change it, so every way of finishing the remaining elements counts. That branch contributes 2 to the power of the number of undecided elements without going deeper.

Approach: Target OR + take-or-skip backtracking

  1. Compute target as the OR of every element in nums.
  2. Define dfs(i, cur), where i is the next index to decide and cur is the OR of the elements taken so far.
  3. If cur == target, return 2^(n - i): each remaining element can be taken or skipped freely.
  4. If i == n, every element is decided and cur falls short, so return 0.
  5. Otherwise return dfs(i + 1, cur | nums[i]) + dfs(i + 1, cur), the counts for taking and for skipping nums[i].
  6. Answer dfs(0, 0). The empty subset is never counted because its OR is 0 and the target is at least 1.

Step-by-step example

nums = [3,1]
  1. target = 3 | 1 = 3.
  2. dfs(0, 0): take 3, so cur = 3. That equals the target with 1 element left undecided, so this branch adds 2^1 = 2 (the subsets [3] and [3,1]).
  3. Back at index 0, skip 3. dfs(1, 0): take 1, cur = 1. At index 2 everything is decided and 1 is not 3, so 0.
  4. Skip 1 as well: cur = 0 at the end, 0 again.
  5. Total 2 + 0 + 0 = 2.

Code: Python, Java, C++

1class Solution:
2    def countMaxOrSubsets(self, nums: List[int]) -> int:
3        target = 0
4        for x in nums:
5            target |= x
6        n = len(nums)
7
8        def dfs(i: int, cur: int) -> int:
9            if cur == target:
10                return 1 << (n - i)
11            if i == n:
12                return 0
13            return dfs(i + 1, cur | nums[i]) + dfs(i + 1, cur)
14
15        return dfs(0, 0)
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(2^n)

In the worst case (for example distinct powers of two) the target is only reached by taking everything, so the recursion visits all 2^n leaves. With n <= 16 that is about 65,000 calls.

Space

O(n)

The recursion is at most n + 1 calls deep and keeps only the index and the running OR in each frame.

Other approaches

Bitmask enumeration (time O(n * 2^n), space O(1))

Loop mask from 1 to 2^n - 1, compute the OR of the chosen elements, and count masks whose OR equals the target. Simple, but each mask costs O(n) unless you reuse the OR of mask without its lowest bit.

DP over OR values (time O(n * V), space O(V))

Keep a map from each reachable OR value to the number of subsets producing it. For each element, add v | x for every existing entry. The answer is the count stored at the target. Fast when few distinct OR values appear.

Common mistakes

  • Counting by value instead of by index. [2,2,2] has 7 different subsets, not 1, because each 2 sits at its own index.
  • Counting the empty subset. It has OR 0, which never equals the target since every element is at least 1, but a bitmask loop that starts at mask 0 must still skip it.
  • Searching for the maximum OR among subsets instead of simply ORing everything. The full array always reaches the maximum.
  • Forgetting that cur can equal the target before the last index. Without the 2^(n - i) shortcut the code is still correct, it just explores more branches.

Frequently asked questions

Why is the maximum OR always the OR of the whole array?

OR only turns bits on. Any subset's OR has a subset of the bits set in the full array's OR, so no subset can exceed it, and the full array itself reaches it.

Why does reaching the target early add 2^(n - i)?

Once cur equals the target, taking more elements cannot add bits the target lacks, and skipping them cannot remove bits already set. Each of the n - i remaining elements has two free choices, giving 2^(n - i) subsets.

Is 2^16 recursion calls really fast enough?

Yes. About 65,000 calls with constant work each runs in a few milliseconds in compiled languages and well under a second in Python.