Number of Steps to Reduce a Number to Zero
Simulation / Bit Counting
Asked at 6 companies
2 recent
- Meta
- Hudson River Trading
- Microsoft
- Bloomberg
- Amazon
Community-reported · source · updated 2026-11-18. ● asked in the last 6 months.
Go step by step: try it yourself before the video and the visual. Progress saves on this device.
0 / 110 pointsSolved (Easy) 0/20Reviews 0/50Mastered 0/25Practice steps 0/15
Step 1
Know these before you start
Practise each one here, or tick what you already know
Watch the concept video if the pattern is new to you.
Step 2
Try it on paper
5–10 minutes, no code yet
ProblemProblem statementGiven an integer num, return the number of steps to reduce it to zero.ReadHide
Problem Statement
Given an integer num, return the number of steps to reduce it to zero.
In one step, if the current number is even, you divide it by 2, otherwise you subtract 1 from it.
Input / Output
Input: num = 14
Output: 6
Explanation: 14 → 7 → 6 → 3 → 2 → 1 → 0
Input: num = 8
Output: 4
Explanation: 8 → 4 → 2 → 1 → 0
Input: num = 123
Output: 12
Constraints
0 ≤ num ≤ 10^6
Understanding the Problem
- Even →
num /= 2(in binary: shift right, drops a0) - Odd →
num -= 1(in binary: clears the lowest1)
Edge Cases
num = 0→ 0 stepsnum = 1→ 1 step
Code it here
Tab / Shift+Tab indent · ⌘/Ctrl+/ comments · ⌘/Ctrl+Enter runs · Esc leaves the editor
Step 6
Get it accepted
All test cases pass
Step 7
Review it so it sticks
Re-solve from memory after
The step that matters most: without reviews most of a solved problem fades within weeks. Three short re-solves, each just as you start to forget, are what make it yours.
Your notes
Three lines in your own words. They come back with this problem's reviews, so you can check yourself after recalling the solution.
Solve it yourself before you look
Reading the solution first feels like progress, but it makes the next similar problem — and the interview version — much harder, because you skipped the part where you figure it out. Give it an honest 20–30 minutes. Stuck? Re-read the pattern, watch the concept video, or try the brute force first.
Hidden: approach · solution code · optimal solution
Approach
Pattern Recognition
Simulation, or think in binary:
- Every
1bit costs one subtract step - Every bit position except the highest costs one divide step
steps = popcount(num) + (bitLength(num) - 1) (num > 0)
Example: 14 = 1110₂ → 3 ones + (4 − 1) = 6 ✅
Approach
Brute Force — Simulation, O(log n)
steps = 0
while num > 0:
if num is even: num /= 2
else: num -= 1
steps++
return steps
Optimized — Bit Counting, O(1) with builtins
if num == 0: return 0
return popcount(num) + bitLength(num) - 1
Complexity
| Approach | Time | Space | Notes |
|---|---|---|---|
| Simulation | O(log n) | O(1) | Straight from the problem statement |
| Bit Counting | O(1)* | O(1) | *Hardware popcount / leading-zero count |
Online Compiler
Solution code
import sys
input = sys.stdin.readline
# ─────────────────────────────────────────────────────────────────────────
# BRUTE FORCE — O(log n) time | O(1) space
# Simulate: even → divide by 2, odd → subtract 1
# ─────────────────────────────────────────────────────────────────────────
def number_of_steps_brute(num):
steps = 0
while num > 0:
if num % 2 == 0:
num //= 2 # even → halve
else:
num -= 1 # odd → drop the last 1-bit
steps += 1
return steps
# ─────────────────────────────────────────────────────────────────────────
# OPTIMIZED — O(1) time | O(1) space
# popcount + (bit length - 1)
# ─────────────────────────────────────────────────────────────────────────
def number_of_steps(num):
if num == 0:
return 0
return bin(num).count("1") + num.bit_length() - 1
# ── Input ─────────────────────────────────────────────────────────────────
num = int(input())
print(number_of_steps(num))Other ways to solve it
- Thought of another approach (slower, or cleverer)? Code it here: the same tests check any approach.
- Community solutions on LeetCode ↗ for many more versions, in many languages. Read them after you've solved it.
Revising this one?
- →Solve it from memory before peeking
- →Say the pattern out loud, not just the code
- →Explain it like you would to a friend
- →Try it again in a different language