Smallest XOR

What u wrote and deleted?

you can click on pencil like logo on upper right to see what i deleted.

@ankur314 Help me with this !

1 Like

Maybe we could take X = A initially and count the number of set bits in B. If A has extra bits, we make the required number of set bits from LSB equal to zero, and if A has less bits, we make required number of zero bits to 1 from LSB.

calculate set bits in A (nA) and B(nB)
if nA == nB then OUTPUT A
if nA > nB then
diff = nA - nB
unset the last diff bits in A
and output ans
if nA < nB
diff = nB - nA
then change rightmost diff unset bits to set bits
and output it

Is it correct?

Rightmost bits seems a little confusing. Are you referring to least significant bits? If yes, then it seems correct!

rightmost means least significant unset bits

1 Like

Remove as many set bits as (as long as nB > 0) you can from A starting from left (if we consider it’s binary representation) as it always better and never makes the answer worse and if nB is still left go from right to left in binary representation of A and if this bit is unset set it, do this until nB > 0.