Number of Steps to Reduce a Number to Zero - Problem

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

In one step, if the current number is even, you have to divide it by 2, otherwise, you have to subtract 1 from it.

Input & Output

Example 1 — Basic Case
$ Input: num = 14
Output: 6
💡 Note: Step-by-step: 14 → 7 → 6 → 3 → 2 → 1 → 0. Total of 6 steps.
Example 2 — Small Number
$ Input: num = 8
Output: 4
💡 Note: 8 → 4 → 2 → 1 → 0. All even numbers except the last step.
Example 3 — Edge Case
$ Input: num = 1
Output: 1
💡 Note: Only one step needed: 1 → 0

Constraints

  • 0 ≤ num ≤ 106

Visualization

Tap to expand
Number Reduction ProcessInput14Reduction RulesIf EVEN: divide by 2 | If ODD: subtract 1Output6Steps: 14 → 7 → 6 → 3 → 2 → 1 → 06 operations total
Understanding the Visualization
1
Input
Start with integer num = 14
2
Process
Apply rules: even ÷ 2, odd - 1
3
Output
Count total steps = 6
Key Takeaway
🎯 Key Insight: Each bit in the binary representation contributes to the step count
Asked in
LeetCode 15 Microsoft 8
91.1K Views
Medium Frequency
~10 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen