<p>Given two positive integers <code>num1</code> and <code>num2</code>, find the positive integer <code>x</code> such that:</p>

<ul>
	<li><code>x</code> has the same number of set bits as <code>num2</code>, and</li>
	<li>The value <code>x XOR num1</code> is <strong>minimal</strong>.</li>
</ul>

<p>Note that <code>XOR</code> is the bitwise XOR operation.</p>

<p>Return <em>the integer </em><code>x</code>. The test cases are generated such that <code>x</code> is <strong>uniquely determined</strong>.</p>

<p>The number of <strong>set bits</strong> of an integer is the number of <code>1</code>&#39;s in its binary representation.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> num1 = 3, num2 = 5
<strong>Output:</strong> 3
<strong>Explanation:</strong>
The binary representations of num1 and num2 are 0011 and 0101, respectively.
The integer <strong>3</strong> has the same number of set bits as num2, and the value <code>3 XOR 3 = 0</code> is minimal.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> num1 = 1, num2 = 12
<strong>Output:</strong> 3
<strong>Explanation:</strong>
The binary representations of num1 and num2 are 0001 and 1100, respectively.
The integer <strong>3</strong> has the same number of set bits as num2, and the value <code>3 XOR 1 = 2</code> is minimal.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= num1, num2 &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution:
    def minimizeXor(self, num1: int, num2: int) -> int:
        # Step 1: Count the number of set bits in num2
        set_bits_num2 = bin(num2).count('1')
        
        # Step 2: Initialize x and the number of set bits in x
        x = 0
        set_bits_x = 0
        
        # Iterate from the most significant bit to the least significant bit
        for bit in range(31, -1, -1):
            if set_bits_x < set_bits_num2 and (num1 & (1 << bit)):
                # If the current bit is set in num1 and we haven't set enough bits in x, set it in x
                x |= (1 << bit)
                set_bits_x += 1
        
        # If we haven't set enough bits, set the remaining bits from the least significant bit upwards
        for bit in range(32):
            if set_bits_x < set_bits_num2 and not (x & (1 << bit)):
                # If the current bit is not set in x and we still need to set more bits, set it
                x |= (1 << bit)
                set_bits_x += 1
        
        return x

# Example usage:
solution = Solution()
print(solution.minimizeXor(3, 5))  # Output: 3
print(solution.minimizeXor(1, 12))  # Output: 3
44