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.

lock icon+12 hidden tests on Submit

challenge icon

Follow-up

Suppose an odd number may also go up by 1 instead of down. 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 cases

Case 1

Case 2

Case 3

Input

n = 14

Expected

6