Ctrl K

Halve or Subtract One

Easy
A counter shows an integer n. Each step changes it in exactly one way: - if the number is even, it is halved; - if it is odd, 1 is subtracted from it. How many steps does it take for the counter to reach 0?
Example 1
n=11 → 6

11 → 10 (odd, subtract 1) → 5 (halve) → 4 → 2 → 1 → 0. Six steps in total.

Example 2
n=16 → 5

Halve four times (16 → 8 → 4 → 2 → 1), then subtract 1 once: 5 steps.

Example 3
n=0 → 0

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

Constraints
  • 0 ≤ n ≤ 10⁶
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSum of Binary Strings EasyOnly Twos Multiplied EasyThe Lost Ticket EasyOnly Fours Multiplied EasyHexadecimal Notation Easy
loading Python…
Input
n=11
Expected
6

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