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
1from it.
Example 1
Input
num = 14Output
6Starting from 14, the sequence of operations is divide, subtract, divide, subtract, divide, subtract, reaching 0 in 6 steps.
Example 2
Input
num = 8Output
4Starting 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