2025-08-23 16:28:34 +0000 UTC
Number of Bit Changes to Make Two Integers Equal
Categories:
Links
Code
class Solution:
def minChanges(self, n: int, k: int) -> int:
res = 0
if n < k:
return -1
while k > 0 or n > 0:
n1, k1 = n & 1, k & 1
if n1 != k1 and n1 == 0 and k1 == 1:
return -1
if n1 != k1:
res += 1
n >>= 1
k >>= 1
return res