<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:
#         count=0
#         for i in bin(num2):
#             if i=="1":
#                 count+=1
#         b1=bin(num1)[2:]
#         index=0
#         while(count>0):
#             if index>=len(b1):
#                 temp="0"*31+bin(num1)[2:]
#                 idx=len(temp)-1
#                 while(count>0):
#                     if temp[idx]=="0":
#                         temp=temp[:idx]+"1"+temp[idx+1:]
#                         count-=1
#                     idx-=1
#                 return int(temp,2)
                
#             if b1[index]=="1":
#                 count-=1
#             index+=1

#         return int(b1[:index]+"0"*(len(b1)-index),2)


class Solution:
    def minimizeXor(self, num1: int, num2: int) -> int:
        count2=0
        for i in bin(num2):
            if i=="1":
                count2+=1
        count1=0
        for i in bin(num1):
            if i=="1":
                count1+=1
        if count1==count2:
            return num1
        elif count1>count2:
            b1=bin(num1)[2:]
            idx=0
            while(count2>0):
                if b1[idx]=="1":
                    count2-=1
                idx+=1
            return int(b1[:idx]+"0"*(len(b1)-idx),2)
        else:
            b1="0"*31+bin(num1)[2:]
            idx=len(b1)-1
            count=count2-count1
            while count>0:
                if b1[idx]=="0":
                    b1=b1[:idx]+"1"+b1[idx+1:]
                    count-=1
                idx-=1
            return int(b1,2)



31