Problem Description
Given three integers a, b, and n, you need to choose an integer x where 0 <= x < 2^n so that the expression (a XOR x) * (b XOR x) is maximized. Since the answer may be large, return the maximum value modulo 10^9 + 7. (Note: XOR denotes the bitwise exclusive or.)
Key Insights
- Only the lower n bits of a and b are affected by choosing x (because 0 <= x < 2^n) while the higher bits remain constant.
- By writing a = a_high2^n + a_low and b = b_high2^n + b_low (where a_low and b_low are the lower n bits), we have:
a XOR x = a_high2^n + (a_low XOR x)
b XOR x = b_high2^n + (b_low XOR x) - Define U = a_low XOR x and observe that (b_low XOR x) can be written as U XOR c, where c = a_low XOR b_low.
- The product becomes:
(a_high2^n + U) * (b_high2^n + (U XOR c)) - The only “free” part is choosing U (or equivalently x) in the range [0, 2^n). Therefore, the optimization reduces to choosing an n‑bit number U so that the resulting product (which also includes linear terms from the fixed higher bits) is maximized.
- For the lower n bits, note that:
- For any bit i where c has 0, both U and (U XOR c) have the same bit. Setting these bits to 1 increases both factors.
- For any bit i where c has 1, the bits in U and (U XOR c) differ; to maximize the product it is best to “balance” the two factors (i.e. try to have numbers as equal as possible). We can do this greedily by processing from the most significant bit to the least significant bit and assigning the 1 to the factor that is smaller so far.
- Finally, once we have computed the optimal U (denoted as u_opt) for the lower n bits, compute the full factors as:
factor1 = (a_high * 2^n) + u_opt
factor2 = (b_high * 2^n) + (u_opt XOR c) and return (factor1 * factor2) % (10^9 + 7).
Space and Time Complexity
Time Complexity: O(n)
Space Complexity: O(1)
Solution
We first split a and b into their higher parts (which remain unchanged when x only affects the lower n bits) and lower parts (which get XORed with x). By defining u = a_low XOR x and noting that b_low XOR x = u XOR (a_low XOR b_low), the problem reduces to choosing an n‑bit number u (denoted as u_opt) to maximize ( (a_high2^n + u) * (b_high2^n + (u XOR c)) ).
For each bit position i from n-1 downto 0:
• If the bit of c in that position is 0, we set the corresponding bit in both u_opt and (u_opt XOR c) to 1 (since adding a 1 to both numbers always increases the product).
• If the bit of c is 1, then the two numbers will differ at that bit. To keep them as balanced as possible, we assign the 1 to the number which is currently smaller. When the two are equal we can choose arbitrarily (our implementation chooses to assign it to the second factor, though assigning to the first is equivalent).
After computing u_opt this way, we obtain the full numbers by adding the fixed higher parts (multiplied by 2^n) back. The final answer is the product of these two numbers modulo 10^9+7.