Menu
CoddyTech

Steps to Reduce a Number to Zero

Start from a non-negative integer n and repeat one rule until it reaches 0: if the number is even, divide it by 2; if it is odd, subtract 1. Each application of the rule is one step. Return the number of steps it takes.

Function

numberOfSteps(n: integer) → integer
ninteger
the starting number
Returnsinteger
the number of steps until the number reaches 0

Constraints

  • 0 ≤ n ≤ 231 - 1

Examples

Input
n = 14
Output
6
Explanation
The number goes 14 → 7 → 6 → 3 → 2 → 1 → 0: three halvings and three subtractions, 6 steps.
challenge icon

Follow-up

Suppose an odd number may also go up by 1. What is the fewest number of steps to reach 0, and which choice is right for 15?

Reset code
def numberOfSteps(n):
    # Write code here

Test #1

Test #2

Test #3

Argument

n = 14

Expected Output

6