Ctrl K

Zero Out With Powers of Two

Medium
You are given a positive integer value. In one step you may add any power of two (1, 2, 4, 8, and so on) to it, or subtract one from it. Return the smallest number of steps needed to turn value into 0.
Example 1
value=23 → 3

23 + 1 = 24, 24 + 8 = 32, 32 − 32 = 0: three steps. Two steps are not enough.

Example 2
value=12 → 2

12 − 4 = 8, then 8 − 8 = 0: two steps.

Example 3
value=64 → 1
Constraints
  • 1 ≤ value ≤ 10⁵
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSteps Down to One MediumStrongest Product Team MediumEdge Toggles on a Tree HardFewest Jumps to the End MediumReach the Last Cell Medium
loading Python…
Input
value=23
Expected
3

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