#2044

Count Number of Maximum Bitwise-OR Subsets

pupil · 365 · lc medium +23 · verified · 89.6% accepted · 1,403 likes · top 99%

Description

Given an integer array nums, compute the maximum achievable bitwise OR across all non-empty subsets. Then count how many distinct non-empty subsets achieve this maximum OR (two subsets are different when they use different index choices, even if element values match).

Example 1:

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]

Example 2:

Input: nums = [2,2,2]
Output: 7
Explanation: All non-empty subsets of [2,2,2] have a bitwise OR of 2. There are 23 - 1 = 7 total subsets.

Example 3:

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]

Code

1
2
3