Minimum Bit Flips to Convert Number

Easy Topics Company Tags

In one operation, choose any binary digit of start and flip it from 0 to 1 or from 1 to 0. Leading zero bits may also be flipped. Return the fewest operations needed to turn start into goal.

Example 1:

Input: start = 8
goal = 9

Output: 1

Explanation: The binary values 1000 and 1001 differ only in the rightmost bit, so one flip is enough.

Example 2:

Input: start = 0
goal = 15

Output: 4

Explanation: To change 0000 into 1111, each of the four displayed bits must be flipped once.

Example 3:

Input: start = 25
goal = 25

Output: 0

Explanation: The starting number already equals the goal, so no bits need to change.

Constraints:

  • 0 <= start, goal <= 1000000000


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.