Minimize XOR
Real-world version: Matching the Notch Count as Closely as You CanThe statement here is our own: the same inputs, outputs and technique.
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.
Examples
Example 1
- Input
- num1 = 8, num2 = 3
- Output
- 9
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
- Input
- num1 = 15, num2 = 1
- Output
- 8
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
- Input
- num1 = 1, num2 = 15
- Output
- 15
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.
Constraints
- 1 <= num1 <= 10^9
- 1 <= num2 <= 10^9
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def minimize_xor(num1: int, num2: int) -> int:public int minimizeXor(int num1, int num2)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 15 test cases: 15 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Minimum Operations to Reduce an Integer to 0Real-world version: Gauge Calibration PressesMedium
- Cinema Seat AllocationReal-world version: Study Pods in the Lecture HallMedium
- Maximum ORReal-world version: Boosting the Channel PanelMedium
- Maximum Xor ProductReal-world version: Toggled Register ProductMedium