Number of Steps to Reduce a Number to Zero (LeetCode 1342): Solution and Explanation

Easy
Math
Bit Manipulation

Start from a non-negative integer num. While it is not zero, halve it if it is even or subtract 1 if it is odd. Return how many operations it takes to reach 0.

Intuition

The rule only ever asks one question: is the number even or odd? In binary that is just the lowest bit, so num & 1 decides every step.

When the lowest bit is 1, subtracting 1 flips it to 0 and leaves every other bit alone. When the lowest bit is 0, dividing by 2 is a right shift: all bits slide one place to the right and the trailing 0 falls off the end.

So the number is being eaten from the right. Each 1-bit is cleared once by a subtraction, and each position below the top bit is shifted away once. That is why a direct simulation is short: it runs about twice the number of bits, at most around 40 times for num up to 10^6.

The same picture gives a closed form for num > 0: popcount(num) + bitLength(num) - 1. The loop is the clearer answer in an interview, and the formula is a good follow-up.

Approach: Simulate with bit operations

  1. Set steps = 0.
  2. While num > 0, look at the lowest bit with num & 1.
  3. If it is 1, subtract 1 from num. Otherwise shift num right by one, which divides it by 2.
  4. Add 1 to steps after either operation.
  5. When num reaches 0, return steps.

Step-by-step example

num = 14
  1. 14 is 1110 in binary. The lowest bit is 0, so halve it: 14 becomes 7 (111). steps = 1.
  2. 7 is odd, so subtract 1: 7 becomes 6 (110). steps = 2.
  3. 6 is even, so shift: 6 becomes 3 (11). steps = 3.
  4. 3 is odd, so subtract 1: 3 becomes 2 (10). steps = 4.
  5. 2 is even, so shift: 2 becomes 1. steps = 5.
  6. 1 is odd, so subtract 1: 1 becomes 0. steps = 6, and the loop ends. Three 1-bits plus three shifts gives 6.

Code: Python, Java, C++

1class Solution:
2    def numberOfSteps(self, num: int) -> int:
3        steps = 0
4        while num > 0:
5            if num & 1:
6                num -= 1
7            else:
8                num >>= 1
9            steps += 1
10        return steps
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(log num)

Each shift removes one bit and each subtraction is followed by a shift (except the last), so the loop runs at most about 2 times the bit length of num.

Space

O(1)

Only the counter and the shrinking number are stored.

Other approaches

Arithmetic simulation (time O(log num), space O(1))

Use num % 2 and num // 2 instead of bit operators. It is the same loop with the same step count, just written in decimal terms.

Bit counting formula (time O(1), space O(1))

For num > 0 the answer is the number of 1-bits plus the bit length minus 1, because every 1-bit needs one subtraction and every lower position needs one shift. Built-ins like bit_count and bit_length make it a one-liner.

Common mistakes

  • Forgetting num = 0. The answer is 0 and the loop must not run; the closed-form formula would give -1 without a special case.
  • Counting the final subtraction twice or forgetting it. The last step always turns 1 into 0 and must be counted once.
  • Writing num & 1 == 1 in languages where == binds tighter than & (Java, C++, C#, JavaScript). Wrap the mask in parentheses.

Frequently asked questions

Why does subtracting 1 from an odd number only change one bit?

An odd number ends in a 1-bit, so subtracting 1 just turns that bit to 0 with no borrow. All higher bits stay the same.

Is there an O(1) solution?

Yes. For num > 0 the step count is popcount(num) + bitLength(num) - 1, and both are single instructions on most hardware. For num = 0 return 0.

How many steps can the largest input take?

10^6 needs 20 bits, so the answer is at most 20 ones plus 19 shifts, 39 steps. The actual answer for 10^6 is 26.