LeetCode solutions

3508. Number of Bit Changes to Make Two Integers Equal

My accepted Python solution to LeetCode problem 3508, Number of Bit Changes to Make Two Integers Equal, running in 4ms.

  • Difficulty: Easy
  • Python
  • Runtime 4ms
  • Memory 17.3MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 4ms, memory 17.3MB, accepted 2026-01-01.

python
class Solution:
    def minChanges(self, n: int, k: int) -> int:
        # Can only change 1 to 0, not 0 to 1
        # So k must have 0s wherever n has 0s (n & k == k)
        if (n & k) != k:
            return -1
        # Count number of 1s in n that are 0 in k
        diff = n ^ k
        return bin(diff).count('1')

Source