2429. Minimize XOR
Difficulty: Medium
Topics: Greedy, Bit Manipulation
Given two positive integers num1 and num2, find the positive integer x such that:
-
xhas the same number of set bits asnum2, and - The value
x XOR num1is 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 <= 109
Hint:
- To arrive at a small xor, try to turn off some bits from num1
- If there are still left bits to set, try to set them from the least significant bit
Solution:
The idea is to manipulate the bits of num1 and match the number of set bits (1s) in num2 while minimizing the XOR result. Here's the step-by-step approach:
Steps:
-
Count the Set Bits in
num2:- Find the number of
1s in the binary representation ofnum2. Let's call thissetBitsCount.
- Find the number of
-
Create a Result Number
x:- Start with
x = 0. - From the binary representation of
num1, preserve the1s in the most significant positions that matchsetBitsCount. - If there are not enough
1s innum1, add extra1s starting from the least significant bit.
- Start with
-
Optimize XOR Result:
- By aligning the set bits of
xwith the most significant1s ofnum1, the XOR value will be minimized.
- By aligning the set bits of
-
Return the Result:
- Return the computed value of
x.
- Return the computed value of
Let's implement this solution in PHP: 2429. Minimize XOR
<?php
/**
* @param Integer $num1
* @param Integer $num2
* @return Integer
*/
function minimizeXor($num1, $num2) {
...
...
...
/**
* go to ./solution.php
*/
}
/**
* Helper function to count the number of set bits in a number
*
* @param $num
* @return int
*/
function countSetBits($num) {
...
...
...
/**
* go to ./solution.php
*/
}
// Test cases
echo minimizeXor(3, 5) . "\n"; // Output: 3
echo minimizeXor(1, 12) . "\n"; // Output: 3
?>
Explanation:
-
countSetBitsFunction:- This function counts the number of
1s in the binary representation of a number using a loop.
- This function counts the number of
-
Building
x:- First, the most significant
1s fromnum1are retained inxto minimize the XOR value. - If more
1s are required to matchsetBitsCount, they are added starting from the least significant bit (to keep the XOR minimal).
- First, the most significant
-
Optimization:
- The XOR value is minimized by aligning the significant bits as much as possible between
num1andx.
- The XOR value is minimized by aligning the significant bits as much as possible between
Complexity:
- Time Complexity: O(32), since the loop iterates a constant number of times (32 bits for integers).
- Space Complexity: O(1), as no extra space is used apart from variables.
Example Walkthrough:
Example 1:
-
num1 = 3 (0011)andnum2 = 5 (0101). -
setBitsCount = 2. -
xstarts as0. - From
num1, keep two most significant1s:x = 3 (0011). -
x XOR num1 = 0, which is minimal.
Example 2:
-
num1 = 1 (0001)andnum2 = 12 (1100). -
setBitsCount = 2. - From
num1, retain1and add one more bit tox:x = 3 (0011). -
x XOR num1 = 2, which is minimal.
This ensures the correct and efficient computation of x.
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!
If you want more helpful content like this, feel free to follow me:
Top comments (0)