You are given an integer array nums.
You need to choose a subset of nums such that the bitwise AND of all numbers in the subset is equal to 0.
Return the maximum possible sum of the elements in the chosen subset.
Note:
230 - 1. And an empty subset has a sum of 0.Example 1:
Input: nums = [5,1,3]
Output: 8
Explanation:
We can choose the subset {5, 3} such that the bitwise AND of all numbers in the subset is equal to 0. The sum of this subset is 5 + 3 = 8, which is the maximum possible sum.
Example 2:
Input: nums = [7,3,2,1,5,8]
Output: 23
Explanation:
We can choose the subset {7, 3, 5, 8} such that the bitwise AND of all numbers in the subset is equal to 0. The sum of this subset is 7 + 3 + 5 + 8 = 23, which is the maximum possible sum.
Example 3:
Input: nums = [8,13,8,10,20]
Output: 41
Explanation:
We can choose the subset {13, 8, 10, 20} such that the bitwise AND of all numbers in the subset is equal to 0. The sum of this subset is 13 + 8 + 10 + 20 = 41, which is the maximum possible sum.
Constraints:
1 <= nums.length <= 10000 <= nums[i] <= 1000When 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:
The goal is to find the largest number that, when combined with every number in a given set using a bitwise AND operation, results in zero. The brute force approach involves checking every possible number within a plausible range to see if it satisfies the condition.
Here's how the algorithm would work step-by-step:
def find_maximum_subset_sum(numbers):
maximum_sum = 0
number_of_numbers = len(numbers)
# Iterate through all possible subsets of the input list.
for i in range(1, 1 << number_of_numbers):
current_subset = []
# Construct the subset based on the bits set in 'i'.
for j in range(number_of_numbers):
if (i >> j) & 1:
current_subset.append(numbers[j])
# Calculate bitwise AND of the current subset
bitwise_and_result = current_subset[0]
for k in range(1, len(current_subset)):
bitwise_and_result &= current_subset[k]
# Check if the bitwise AND result is zero.
if bitwise_and_result == 0:
#If bitwise AND is zero, calculate sum.
current_sum = sum(current_subset)
#Update maximum sum with the current sum if larger.
if current_sum > maximum_sum:
maximum_sum = current_sum
return maximum_sumThe goal is to find the largest number less than a given input, where that number, when combined with every number in a given set using a bitwise operation, results in zero. Instead of checking every possible number, we focus on building the result from the 'opposite' bits found in the set of numbers.
Here's how the algorithm would work step-by-step:
def maximum_number_that_makes_result_of_bitwise_and_zero(numbers):
maximum_possible_number = max(numbers)
number_of_inputs = len(numbers)
total_integers = maximum_possible_number + 1
count = 0
for mask in range(1, 1 << number_of_inputs):
current_intersection = total_integers - 1
subset_size = 0
for j in range(number_of_inputs):
if (mask >> j) & 1:
#Determine intersection based on current bit
current_intersection &= ~numbers[j]
subset_size += 1
if subset_size % 2 == 1:
# Inclusion-exclusion principle: Add for odd subset sizes
count += current_intersection + 1
else:
# Inclusion-exclusion principle: Subtract for even subset sizes
count -= current_intersection + 1
# The final result is derived by subtracting the calculated count.
return total_integers - count -1| Case | How to Handle |
|---|---|
| Null or empty input array | Return the maximum possible integer value since the condition is trivially satisfied for any x. |
| Array with a single element | Return the bitwise complement of the single element, ensuring it's within the integer range. |
| All elements in the array are zero | Return the maximum possible integer value as the bitwise AND will always be zero. |
| Array contains only the maximum integer value | Return zero, as any other number's bitwise AND with the maximum integer will not be zero. |
| Large input array (performance considerations) | Iterate through the array to calculate the bitwise OR of all elements, then return the complement. |
| Integer overflow when calculating the complement | Use a bitmask to ensure the result remains within the valid integer range. |
| Array with mixed small and large numbers | The bitwise OR operation will handle the mixture of small and large numbers correctly when computing the complement. |
| No valid solution smaller than MAX_INT (should always be possible since x=0 is a valid result) | The solution ensures that at least x=0 is a viable option, and aims for the largest possible that satisfies the conditions. |