All problems
1121EasyBit Manipulation

Throws Between Two Switch Banks

Tracked in this browser only
Write code

Trains the technique from

LeetCode 2220Minimum Bit Flips to Convert Number

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

Two banks of switches are described by the whole numbers start and goal. A switch is on wherever the number carries a 1 bit, switches are numbered from the lowest bit upwards, and any place beyond a number's own bits counts as off.

One throw flips a single switch. Return the fewest throws that turn the start bank into the goal bank.

Examples

Example 1

Input
start = 15, goal = 240
Output
8

The lowest four switches are on in the first bank and off in the second, and the next four are the other way round, so all eight have to be thrown.

Example 2

Input
start = 1023, goal = 1024
Output
11

The first bank has its lowest ten switches on while the second has only the eleventh, so ten switches go off and one comes on.

Example 3

Input
start = 1, goal = 1
Output
0

The two banks already match, so nothing is thrown.

Constraints

  • 0 <= start <= 10^9
  • 0 <= goal <= 10^9

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def min_bit_flips(start: int, goal: int) -> int:
Java
public int minBitFlips(int start, int goal)
September 7
Apply