Count Number of Maximum Bitwise-OR Subsets - Problem
Given an integer array
A subset is any collection of elements from the original array (including empty collections, but we only consider non-empty subsets here). Two subsets are considered different if they contain elements at different indices.
The bitwise OR operation combines bits using the rule: if either bit is 1, the result is 1. For an array, we OR all elements together:
Example: For
nums, you need to find the maximum possible bitwise OR of any subset of the array, and then count how many different subsets achieve this maximum OR value.A subset is any collection of elements from the original array (including empty collections, but we only consider non-empty subsets here). Two subsets are considered different if they contain elements at different indices.
The bitwise OR operation combines bits using the rule: if either bit is 1, the result is 1. For an array, we OR all elements together:
a[0] OR a[1] OR ... OR a[n-1].Example: For
[3, 1], possible subsets are [3] (OR = 3), [1] (OR = 1), and [3,1] (OR = 3). Maximum OR is 3, achieved by 2 subsets. Input & Output
example_1.py โ Basic case
$
Input:
[3, 1]
โบ
Output:
2
๐ก Note:
Maximum OR is 3. Subsets with OR = 3: [3] and [3,1]. Count = 2.
example_2.py โ Multiple elements
$
Input:
[2, 2, 2]
โบ
Output:
7
๐ก Note:
Maximum OR is 2. All non-empty subsets have OR = 2, so count = 7 (2^3 - 1).
example_3.py โ Mixed values
$
Input:
[3, 2, 1, 5]
โบ
Output:
6
๐ก Note:
Maximum OR is 7 (3|2|1|5). Subsets achieving OR = 7 must include elements that contribute unique bits.
Visualization
Tap to expand
Understanding the Visualization
1
Analyze Devices
Each device has features: 3=[11], 1=[01], 2=[10]
2
Find Max Features
Maximum features = 11|01|10 = 11 (binary) = 3 (decimal)
3
Count Combinations
Find all device combinations that achieve maximum features
4
Result
Multiple combinations can achieve the same maximum feature set
Key Takeaway
๐ฏ Key Insight: The maximum possible bitwise OR is always achieved by combining all array elements. We only need to count how many subsets reach this theoretical maximum.
Time & Space Complexity
Time Complexity
O(2^n)
We still need to explore all possible subsets in worst case, but with some pruning potential
โ Quadratic Growth
Space Complexity
O(n)
Recursion stack depth can go up to n levels deep
โก Linearithmic Space
Constraints
- 1 โค nums.length โค 16
- 1 โค nums[i] โค 105
- The array length is small (โค16) making exponential solutions feasible
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code