Ctrl K

Switch Panel Toggles

Easy
A panel holds a row of switches. Its state is recorded as a number: written in binary, a 1 means the switch is on and a 0 means it is off (the rightmost bit is the first switch). The row is as long as needed, so leading zeros are switches that are off. One action toggles exactly one switch. Given the current state current and the desired state target, return the minimum number of toggles that turns current into target.
Example 1
current=12, target=5 → 2

12 is 1100 and 5 is 0101. They differ in the first and the fourth bits from the right, so 2 toggles are needed.

Example 2
current=0, target=7 → 3

7 is 111, so three switches have to be turned on.

Example 3
current=9, target=9 → 0
Constraints
  • 0 ≤ current, target ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSum of Binary Strings EasyThe Unpaired Value EasyFlip the Bit Order EasyCount the Set Bits EasySize of a Packed Tree Easy
loading Python…
Input
current=12, target=5
Expected
2

Run checks the first 3 tests in your browser. Submit checks all 69 tests on the server, hidden ones included.