Trains the technique from
LeetCode 2429Minimize XORThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
Two whole numbers num1 and num2 are given. Build a whole number x carrying exactly as many 1 bits as num2 does, chosen so that the bitwise exclusive-or of x and num1 comes out as small as possible.
Return x. Exactly one number meets both demands.
Example 1
Two bits are allowed. Keeping the bit num1 already carries costs nothing, and the cheapest home for the second is the lowest free place.
Example 2
Only one bit is allowed, so three of num1's four bits have to go. Dropping the highest would cost more than dropping all the others, so that is the one kept.
Example 3
Four bits are needed and num1 supplies one of them. The other three take the lowest free places, which is the smallest number with four bits.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def minimize_xor(num1: int, num2: int) -> int:public int minimizeXor(int num1, int num2)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.