Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Move from brute-force thinking to an efficient approach using math strategy.
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 < 2500 <= n <= 50Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math · Greedy · Bit Manipulation
12 5 4
6 7 5
1 6 3
maximum-xor-after-operations)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2939: Maximum Xor Product
class Solution {
public int maximumXorProduct(long a, long b, int n) {
final int mod = (int) 1e9 + 7;
long ax = (a >> n) << n;
long bx = (b >> n) << n;
for (int i = n - 1; i >= 0; --i) {
long x = a >> i & 1;
long y = b >> i & 1;
if (x == y) {
ax |= 1L << i;
bx |= 1L << i;
} else if (ax < bx) {
ax |= 1L << i;
} else {
bx |= 1L << i;
}
}
ax %= mod;
bx %= mod;
return (int) (ax * bx % mod);
}
}
// Accepted solution for LeetCode #2939: Maximum Xor Product
func maximumXorProduct(a int64, b int64, n int) int {
const mod int64 = 1e9 + 7
ax := (a >> n) << n
bx := (b >> n) << n
for i := n - 1; i >= 0; i-- {
x, y := (a>>i)&1, (b>>i)&1
if x == y {
ax |= 1 << i
bx |= 1 << i
} else if ax < bx {
ax |= 1 << i
} else {
bx |= 1 << i
}
}
ax %= mod
bx %= mod
return int(ax * bx % mod)
}
# Accepted solution for LeetCode #2939: Maximum Xor Product
class Solution:
def maximumXorProduct(self, a: int, b: int, n: int) -> int:
mod = 10**9 + 7
ax, bx = (a >> n) << n, (b >> n) << n
for i in range(n - 1, -1, -1):
x = a >> i & 1
y = b >> i & 1
if x == y:
ax |= 1 << i
bx |= 1 << i
elif ax > bx:
bx |= 1 << i
else:
ax |= 1 << i
return ax * bx % mod
// Accepted solution for LeetCode #2939: Maximum Xor Product
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #2939: Maximum Xor Product
// class Solution {
// public int maximumXorProduct(long a, long b, int n) {
// final int mod = (int) 1e9 + 7;
// long ax = (a >> n) << n;
// long bx = (b >> n) << n;
// for (int i = n - 1; i >= 0; --i) {
// long x = a >> i & 1;
// long y = b >> i & 1;
// if (x == y) {
// ax |= 1L << i;
// bx |= 1L << i;
// } else if (ax < bx) {
// ax |= 1L << i;
// } else {
// bx |= 1L << i;
// }
// }
// ax %= mod;
// bx %= mod;
// return (int) (ax * bx % mod);
// }
// }
// Accepted solution for LeetCode #2939: Maximum Xor Product
function maximumXorProduct(a: number, b: number, n: number): number {
const mod = BigInt(1e9 + 7);
let ax = (BigInt(a) >> BigInt(n)) << BigInt(n);
let bx = (BigInt(b) >> BigInt(n)) << BigInt(n);
for (let i = BigInt(n - 1); ~i; --i) {
const x = (BigInt(a) >> i) & 1n;
const y = (BigInt(b) >> i) & 1n;
if (x === y) {
ax |= 1n << i;
bx |= 1n << i;
} else if (ax < bx) {
ax |= 1n << i;
} else {
bx |= 1n << i;
}
}
ax %= mod;
bx %= mod;
return Number((ax * bx) % mod);
}
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.