Number of Steps to Reduce a Number to Zero

Given an integer num, return the number of steps needed to bring it down to zero. In one step, if the current number is even you divide it by 2, otherwise you subtract 1 from it.

30:00

Number of Steps to Reduce a Number to Zero
easy
Free written solution
Topics
Companies

Given an integer num, return the number of steps needed to bring it down to zero. In one step, if the current number is even you divide it by 2, otherwise you subtract 1 from it.

Example 1:
Input: {"num":14}
Output: 6
Constraints:
  • 0≤num≤1060 \leq \text{num} \leq 10^6

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"num":14}

num = 14 is 1110 in binary: 3 one-bits to clear and 3 shifts to empty the number.

num =

14

1

3

1

2

1

1

0

0

out

lowest bit decides

odd: subtract 1 clears it

even: shift right drops it

steps = 0

-1 x 0 /2 x 0

no steps yet

Variables
VariableValue
num14
binary1110
DepthFunction Call
Stack empty
0/15