shlogg · Early preview
Md Ariful Haque @mah-shamim

Minimizing XOR With Bit Manipulation

Minimize XOR: Count set bits in num2, preserve most significant 1s in num1, add extra 1s from least significant bit. Return x with minimal XOR value.

2429. Minimize XOR
Difficulty: Medium
Topics: Greedy, Bit Manipulation
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.
Example 1:

Input: num1 = 3, num2 = 5
Output: 3
Explanation: The binary representations of num1 and num2 are 0011 and 0101, respectively.

The int...