Given an integer array nums, find the maximum possible bitwise OR of a subset of nums and return the number of different non-empty subsets with the maximum bitwise OR.
An array a is a subset of an array b if a can be obtained from b by deleting some (possibly zero) elements of b. Two subsets are considered different if the indices of the elements chosen are different.
The bitwise OR of an array a is equal to a[0] OR a[1] OR ... OR a[a.length - 1] (0-indexed).
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]
Constraints:
1 <= nums.length <= 161 <= nums[i] <= 105When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
To find how many groups of items can be formed to reach the highest possible combined value, we can simply test every single possible group combination that can be made from the given collection. By looking at every possible grouping, we can calculate each group's combined value, find the overall maximum value, and count how many groups achieved that maximum.
Here's how the algorithm would work step-by-step:
class Solution:
def countMaxOrSubsets(self, nums: list[int]) -> int:
from itertools import combinations
all_subsets = []
for subset_size in range(1, len(nums) + 1):
for current_combination in combinations(nums, subset_size):
all_subsets.append(current_combination)
calculated_values = []
for subset_group in all_subsets:
current_bitwise_or = 0
for number in subset_group:
current_bitwise_or |= number
calculated_values.append(current_bitwise_or)
# We need to find the absolute highest bitwise-OR value across all generated subsets
maximum_bitwise_or = max(calculated_values)
maximum_count = 0
for group_value in calculated_values:
if group_value == maximum_bitwise_or:
maximum_count += 1
return maximum_countTo find the subsets that achieve the highest possible bitwise-OR value, we first need to figure out what that maximum possible value is by combining every single number together. Then, we systematically explore the different combinations of numbers to see how many of them reach that exact maximum target.
Here's how the algorithm would work step-by-step:
class Solution:
def count_max_or_subsets(self, numbers_list: list[int]) -> int:
maximum_target_value = 0
for number in numbers_list:
maximum_target_value |= number
successful_matches_tally = 0
def explore_combinations(current_index: int, current_bitwise_or: int):
nonlocal successful_matches_tally
# We stop exploring further when all numbers have been considered.
if current_index == len(numbers_list):
if current_bitwise_or == maximum_target_value:
successful_matches_tally += 1
return
# Branch out by including the current number in our combination group.
explore_combinations(current_index + 1, current_bitwise_or | numbers_list[current_index])
# Branch out again by leaving the current number out of our group.
explore_combinations(current_index + 1, current_bitwise_or)
explore_combinations(0, 0)
return successful_matches_tally| Case | How to Handle |
|---|---|
| Array of minimum size with exactly one element | The algorithm correctly identifies this single element as both the maximum OR value and the sole subset achieving it, returning a count of one. |
| Inputs containing zeros | Since bitwise OR with zero does not change the value, including zeros will duplicate the maximum OR count and must be properly factored into the subset combinations. |
| Maximum-sized inputs leading to exponential subset growth | Because the maximum length of nums typically reaches up to sixteen in this problem, a recursive backtracking approach scales efficiently with two to the power of N complexity. |
| Inputs with all identical values | The bitwise OR will simply equal that single value, and the subset count will equal two to the power of N minus one since every non-empty combination yields the same OR. |
| Subsets producing duplicate index combinations but identical values | The backtracking algorithm naturally evaluates unique index combinations rather than unique values, ensuring every distinct subset is counted independently. |
| Large integer values within standard bitwise limits | Standard thirty-two bit integer types natively support the bitwise OR operations without overflow risks since values fit well within standard integer primitives. |