Given three integers a
, b
, and n
, return the maximum value of (a XOR x) * (b XOR x)
where 0 <= x < 2n
.
Since the answer may be too large, return it modulo 109 + 7
.
Note that XOR
is the bitwise XOR operation.
Example 1:
Input: a = 12, b = 5, n = 4
Output: 98
Explanation: For x = 2, (a XOR x) = 14 and (b XOR x) = 7. Hence, (a XOR x) * (b XOR x) = 98.
It can be shown that 98 is the maximum value of (a XOR x) * (b XOR x) for all 0 <= x < 2n.
Example 2:
Input: a = 6, b = 7 , n = 5 Output: 930 Explanation: For x = 25, (a XOR x) = 31 and (b XOR x) = 30. Hence, (a XOR x) * (b XOR x) = 930. It can be shown that 930 is the maximum value of (a XOR x) * (b XOR x) for all 0 <= x < 2n.
Example 3:
Input: a = 1, b = 6, n = 3 Output: 12 Explanation: For x = 5, (a XOR x) = 4 and (b XOR x) = 3. Hence, (a XOR x) * (b XOR x) = 12. It can be shown that 12 is the maximum value of (a XOR x) * (b XOR x) for all 0 <= x < 2n.
Constraints:
0 <= a, b < 250
0 <= n <= 50
When 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 brute force strategy for the maximum XOR product problem involves checking all possible combinations of bit flips in the input numbers. For each possible combination, we compute the XOR product and then choose the combination that gives the largest XOR product. It's like trying out every single possibility to see which one works best.
Here's how the algorithm would work step-by-step:
def maximum_xor_product_brute_force(first_number, second_number, maximum_bit_flips):
maximum_xor_product = 0
# Iterate through all possible combinations of bit flips
for first_number_bit_flips in range(maximum_bit_flips + 1):
for second_number_bit_flips in range(maximum_bit_flips - first_number_bit_flips + 1):
#Try all combinations of flipping i bits in first_number and j bits in second_number
for first_number_mask in range(1 << first_number_bit_flips):
for second_number_mask in range(1 << second_number_bit_flips):
modified_first_number = first_number
modified_second_number = second_number
bit_counter = 0
temporary_mask = first_number_mask
# Flip the bits in the first number according to the mask
while temporary_mask > 0:
if temporary_mask & 1:
modified_first_number ^= (1 << bit_counter)
temporary_mask >>= 1
bit_counter += 1
bit_counter = 0
temporary_mask = second_number_mask
# Flip the bits in the second number according to the mask
while temporary_mask > 0:
if temporary_mask & 1:
modified_second_number ^= (1 << bit_counter)
temporary_mask >>= 1
bit_counter += 1
current_xor_product = (modified_first_number ^ modified_second_number)
# Update maximum if current product is larger
if current_xor_product > maximum_xor_product:
maximum_xor_product = current_xor_product
return maximum_xor_product
The goal is to maximize the product of two XOR operations. The key insight is to manipulate the input numbers to make each XOR result as large as possible, prioritizing setting the most significant bits to 1. We achieve this by strategically adding or subtracting from the inputs based on the constraints.
Here's how the algorithm would work step-by-step:
def maximum_xor_product(number_one, number_two, limit_a, limit_b):
max_product = (number_one ^ number_two) * (number_one ^ number_two)
number_one_increase = min(limit_a, number_one + (limit_a - number_one)) if number_one < limit_a else number_one
number_one_decrease = max(0, number_one - number_one) if number_one > 0 else number_one
number_two_increase = min(limit_b, number_two + (limit_b - number_two)) if number_two < limit_b else number_two
number_two_decrease = max(0, number_two - number_two) if number_two > 0 else number_two
# Try increasing number_one and decreasing number_two.
if number_one < limit_a and number_two > 0:
product = (number_one_increase ^ number_two_decrease) * (number_one_increase ^ number_two_decrease)
max_product = max(max_product, product)
# Try decreasing number_one and increasing number_two.
if number_one > 0 and number_two < limit_b:
product = (number_one_decrease ^ number_two_increase) * (number_one_decrease ^ number_two_increase)
max_product = max(max_product, product)
# Try increasing both numbers.
if number_one < limit_a and number_two < limit_b:
product = (number_one_increase ^ number_two_increase) * (number_one_increase ^ number_two_increase)
max_product = max(max_product, product)
# Try decreasing both numbers.
if number_one > 0 and number_two > 0:
product = (number_one_decrease ^ number_two_decrease) * (number_one_decrease ^ number_two_decrease)
max_product = max(max_product, product)
# Increasing only number_one.
if number_one < limit_a:
product = (number_one_increase ^ number_two) * (number_one_increase ^ number_two)
max_product = max(max_product, product)
# Decreasing only number one
if number_one > 0:
product = (number_one_decrease ^ number_two) * (number_one_decrease ^ number_two)
max_product = max(max_product, product)
# Increasing only number two
if number_two < limit_b:
product = (number_one ^ number_two_increase) * (number_one ^ number_two_increase)
max_product = max(max_product, product)
# Decreasing only number two
if number_two > 0:
product = (number_one ^ number_two_decrease) * (number_one ^ number_two_decrease)
max_product = max(max_product, product)
# We need to return the maximum value.
return max_product
Case | How to Handle |
---|---|
n = 0 | If n is zero, no operations are allowed, so return a * b modulo 10^9 + 7 directly. |
a = 0 or b = 0 | If a or b is initially zero, prioritize maximizing the other number to maximize the product. |
a = b | If a equals b, try to maximize one and minimize the other within the constraints of n, since a large difference may yield a higher product. |
Large a and b, close to 2^n - 1 | Consider that XORing close to 2^n-1 value with x can decrease its value but might increase the other value more, affecting their product; handle accordingly with modular arithmetic to prevent overflow. |
Large n, potential for many XOR operations | The number of possible XOR operations can be large, requiring careful decision-making regarding which number to XOR. |
a and b are very small | If a and b are small, the objective would be to raise them to their maximum values possible using XOR. |
Integer overflow during product calculation | Apply modulo 10^9 + 7 at each multiplication step to prevent integer overflow. |
Negative a or b (or both) | Since the problem does not explicitly state that a and b are non-negative, treat a and b as signed integers, so we can choose to XOR a and b such that the result of the XOR becomes positive to improve the product. |