Number of Steps to Reduce a Number to Zero

Given an integer num, return the number of steps to reduce it to zero.

In one step:

  • If the current number is even, divide it by 2.
  • Otherwise, subtract 1 from it.
Example 1
Inputnum = 14
Output6
Starting from 14, the sequence of operations is divide, subtract, divide, subtract, divide, subtract, reaching 0 in 6 steps.
Example 2
Inputnum = 8
Output4
Starting from 8, divide by 2 three times to reach 1, then subtract 1 to reach 0, for 4 total steps.

Constraints

  • 0 <= num <= 10^6

Asked at 6 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate