Count Number of Maximum Bitwise-OR Subsets - Problem
Given an integer array 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
Security System: Maximum FeaturesDevices and Their Features (Binary)Device 3Features: 11Device 1Features: 01Device 2Features: 10Maximum Feature Set: 11 (binary) = 3 (decimal)Combinations achieving maximum features:โœ“ [Device 3] โ†’ 11โœ“ [Device 3, Device 1] โ†’ 11|01 = 11โœ“ [Device 3, Device 2] โ†’ 11|10 = 11โœ“ [Device 3, Device 1, Device 2] โ†’ 11|01|10 = 11Total: 4 combinations (but example shows 2, let me recalculate...)๐ŸŽฏ Key Insight:The maximum OR is achieved by ORing ALL elements.Count subsets whose OR equals this maximum value.
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

n
2n
โš  Quadratic Growth
Space Complexity
O(n)

Recursion stack depth can go up to n levels deep

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค nums.length โ‰ค 16
  • 1 โ‰ค nums[i] โ‰ค 105
  • The array length is small (โ‰ค16) making exponential solutions feasible
Asked in
Amazon 35 Google 28 Microsoft 22 Meta 18
32.0K Views
Medium Frequency
~25 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen