Problem
Given two positive integers num1
and num2
, find the positive integer x
such that:
x
has the same number of set bits asnum2
, 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.
Examples
Example 1:
|
|
Example 2:
|
|
Constraints:
1 <= num1, num2 <= 10^9
Solution
Method 1 - Count the bits in num2 and minimize xor
Intuition
Given, num1
and num2
, we need to find a positive integer x
such that
x
has the same number of set bits asnum2
, i.e.bitCount(x)
==bitCount(num2)
x ^ num1
is minimal ⇨ set bits inx
should align with those innum1
, i.e.x
should have set bits in positions wherenum1
has1s
as well
If we XOR two bits, the result is 1 if the bits are different but 0 if the bits are the same. For example:
|
|
So, we want to minimize, hence we try to set the bits at the position, where num1
has set bits as well.
Now, there are 3 cases. Let a = bitCoun(num1)
and b = bitCount(num2)
(be number of set bits in num2
).
Cases
Case 1 - b
< a
Suppose, num2
has 3 set bits and num1
can have 5 set bits. So, in x
we want to set 3 set bits, and which one should we set? What will have more impact when we perform x ^ num1
, when we are nullifying the value, and answer is setting the bits at MSB. So, we will loop from bit position 31
to 0 for that.
Case 2 - b
> a
Suppose, num2
has 5 set bits and num1
can have 3 set bits. So, in x
we want to set 5 set bits. So, we set the 3 bits where num1
has a set bit, and now we still have 2 remaining bits we want to set in x
. Where should we set them? And the answer is since we want the minimal answer, we should set them at positions, where they contribute as minimum as possible, i.e. LSB positions, and hence we loop from bit position 0.
Case 3 - b
== a
This is the simplest case. Just set the bits as placed in num1
.
Approach
- Count Set Bits in num2: Calculate the number of set bits (1s) in
num2
and store it incnt
. We can use methods described in Number of 1 Bits or use standard library function for that. - Set Common Bits in the Result:
- To minimize
x XOR num1
, align set bits ofx
with those innum1
. Iterate from the most significant bit (MSB) to the least significant bit (LSB) ofnum1
:- If a bit in
num1
is set andcnt
is greater than 0, set the corresponding bit inx
and decrementcnt
.
- If a bit in
- To minimize
- Set Remaining Bits:
- If there are still bits left to be set in
x
(cnt > 0
), set them from LSB to MSB:- If a bit position in
num1
is not set andcnt
is greater than 0, set this bit inx
and decrementcnt
.
- If a bit position in
- If there are still bits left to be set in
Video explanation
Here is the video explaining this method in detail. Please check it out:
Code
|
|
|
|
Complexity
- ⏰ Time complexity:
O(1)
as we iterate through number of bits from right to left first and left to right later. ORO(log n)
if we want to right it in terms of numbers. - 🧺 Space complexity:
O(1)