Skip to content

Practice Number of Bit Changes to Make Two Integers Equal

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Easy#3226
LeetCode Problem

Number of Bit Changes to Make Two Integers Equal

Problem Statement

You are given two positive integers n and k. You can choose any bit in the binary representation of n that is equal to 1 and change it to 0. Return the number of changes needed to make n equal to k. If it is impossible, return -1.

Source: Number of Bit Changes to Make Two Integers Equal on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
n = 13, k = 4
Output
2

# Example 2

Input
n = 21, k = 21
Output
0

# Example 3

Input
n = 14, k = 13
Output
-1

Constraints

1 <= n, k <= 10^6

Related LeetCode Problems

Practice more problems similar to Number of Bit Changes to Make Two Integers Equal.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.