Ctrl K

Steps Down to One

Medium
You are given a positive integer value. In one step you may: - halve the number if it is even; - add 1 to it or subtract 1 from it (your choice) if it is odd. Return the smallest number of steps needed to turn value into 1.
Example 1
value=12 → 4

12 → 6 → 3 → 2 → 1: four steps.

Example 2
value=15 → 5

15 → 16 → 8 → 4 → 2 → 1: adding one beats subtracting here.

Example 3
value=1 → 0

The number is already 1, so no steps are needed.

Constraints
  • 1 ≤ value ≤ 2³¹ − 1
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsShared Pool Race to a Goal MediumCheapest Basket with Bundles MediumGroups With Equal Sums MediumBinary Number Under a Cap MediumZero Out With Powers of Two Medium
loading Python…
Input
value=12
Expected
4

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