Count Number of Maximum Bitwise-OR Subsets (LeetCode 2044): Solution and Explanation
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
- Compute
targetas the OR of every element innums. - Define
dfs(i, cur), whereiis the next index to decide andcuris the OR of the elements taken so far. - If
cur == target, return2^(n - i): each remaining element can be taken or skipped freely. - If
i == n, every element is decided andcurfalls short, so return 0. - Otherwise return
dfs(i + 1, cur | nums[i]) + dfs(i + 1, cur), the counts for taking and for skippingnums[i]. - 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]
- target = 3 | 1 = 3.
- 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]).
- 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.
- Skip 1 as well: cur = 0 at the end, 0 again.
- 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)1class Solution {
2 private int[] nums;
3 private int target;
4
5 public int countMaxOrSubsets(int[] nums) {
6 this.nums = nums;
7 target = 0;
8 for (int x : nums) {
9 target |= x;
10 }
11 return dfs(0, 0);
12 }
13
14 private int dfs(int i, int cur) {
15 if (cur == target) {
16 return 1 << (nums.length - i);
17 }
18 if (i == nums.length) {
19 return 0;
20 }
21 return dfs(i + 1, cur | nums[i]) + dfs(i + 1, cur);
22 }
23}1class Solution {
2public:
3 int countMaxOrSubsets(vector<int>& nums) {
4 int target = 0;
5 for (int x : nums) {
6 target |= x;
7 }
8 return dfs(nums, target, 0, 0);
9 }
10
11private:
12 int dfs(const vector<int>& nums, int target, int i, int cur) {
13 if (cur == target) {
14 return 1 << ((int) nums.size() - i);
15 }
16 if (i == (int) nums.size()) {
17 return 0;
18 }
19 return dfs(nums, target, i + 1, cur | nums[i]) + dfs(nums, target, i + 1, cur);
20 }
21};Time and space complexity
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.
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.