Using greedy without proof
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.
Move from brute-force thinking to an efficient approach using greedy strategy.
Given two positive integers num1 and num2, find the positive integer x such that:
x has the same number of set bits as num2, andx XOR num1 is minimal.Note that XOR is the bitwise XOR operation.
Return the integer x. The test cases are generated such that x is uniquely determined.
The number of set bits of an integer is the number of 1's in its binary representation.
Example 1:
Input: num1 = 3, num2 = 5
Output: 3
Explanation:
The binary representations of num1 and num2 are 0011 and 0101, respectively.
The integer 3 has the same number of set bits as num2, and the value 3 XOR 3 = 0 is minimal.
Example 2:
Input: num1 = 1, num2 = 12
Output: 3
Explanation:
The binary representations of num1 and num2 are 0001 and 1100, respectively.
The integer 3 has the same number of set bits as num2, and the value 3 XOR 1 = 2 is minimal.
Constraints:
1 <= num1, num2 <= 109Problem summary: Given two positive integers num1 and num2, find the positive integer x such that: x has the same number of set bits as num2, and The value x XOR num1 is minimal. Note that XOR is the bitwise XOR operation. Return the integer x. The test cases are generated such that x is uniquely determined. The number of set bits of an integer is the number of 1's in its binary representation.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Greedy · Bit Manipulation
3 5
1 12
maximum-xor-of-two-numbers-in-an-array)maximum-xor-with-an-element-from-array)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2429: Minimize XOR
class Solution {
public int minimizeXor(int num1, int num2) {
int cnt = Integer.bitCount(num2);
int x = 0;
for (int i = 30; i >= 0 && cnt > 0; --i) {
if ((num1 >> i & 1) == 1) {
x |= 1 << i;
--cnt;
}
}
for (int i = 0; cnt > 0; ++i) {
if ((num1 >> i & 1) == 0) {
x |= 1 << i;
--cnt;
}
}
return x;
}
}
// Accepted solution for LeetCode #2429: Minimize XOR
func minimizeXor(num1 int, num2 int) int {
cnt := bits.OnesCount(uint(num2))
x := 0
for i := 30; i >= 0 && cnt > 0; i-- {
if num1>>i&1 == 1 {
x |= 1 << i
cnt--
}
}
for i := 0; cnt > 0; i++ {
if num1>>i&1 == 0 {
x |= 1 << i
cnt--
}
}
return x
}
# Accepted solution for LeetCode #2429: Minimize XOR
class Solution:
def minimizeXor(self, num1: int, num2: int) -> int:
cnt = num2.bit_count()
x = 0
for i in range(30, -1, -1):
if num1 >> i & 1 and cnt:
x |= 1 << i
cnt -= 1
for i in range(30):
if num1 >> i & 1 ^ 1 and cnt:
x |= 1 << i
cnt -= 1
return x
// Accepted solution for LeetCode #2429: Minimize XOR
impl Solution {
pub fn minimize_xor(num1: i32, mut num2: i32) -> i32 {
let mut cnt = 0;
while num2 > 0 {
num2 -= num2 & -num2;
cnt += 1;
}
let mut x = 0;
let mut c = cnt;
for i in (0..=30).rev() {
if c > 0 && (num1 >> i) & 1 == 1 {
x |= 1 << i;
c -= 1;
}
}
for i in 0..=30 {
if c == 0 {
break;
}
if ((num1 >> i) & 1) == 0 {
x |= 1 << i;
c -= 1;
}
}
x
}
}
// Accepted solution for LeetCode #2429: Minimize XOR
function minimizeXor(num1: number, num2: number): number {
let cnt = 0;
while (num2) {
num2 &= num2 - 1;
++cnt;
}
let x = 0;
for (let i = 30; i >= 0 && cnt > 0; --i) {
if ((num1 >> i) & 1) {
x |= 1 << i;
--cnt;
}
}
for (let i = 0; cnt > 0; ++i) {
if (!((num1 >> i) & 1)) {
x |= 1 << i;
--cnt;
}
}
return x;
}
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: 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.