Skip to main content

Number of Steps to Reduce a Number to Zero - Solution & Explanation

EasyMathBit Manipulation11 min readAsked at: Amazon, Microsoft, Meta +3
Practice this problem

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 have to divide it by 2, otherwise, you have to subtract 1 from it.

 

Example 1:

Input: num = 14
Output: 6
Explanation: 
Step 1) 14 is even; divide by 2 and obtain 7. 
Step 2) 7 is odd; subtract 1 and obtain 6.
Step 3) 6 is even; divide by 2 and obtain 3. 
Step 4) 3 is odd; subtract 1 and obtain 2. 
Step 5) 2 is even; divide by 2 and obtain 1. 
Step 6) 1 is odd; subtract 1 and obtain 0.

Example 2:

Input: num = 8
Output: 4
Explanation: 
Step 1) 8 is even; divide by 2 and obtain 4. 
Step 2) 4 is even; divide by 2 and obtain 2. 
Step 3) 2 is even; divide by 2 and obtain 1. 
Step 4) 1 is odd; subtract 1 and obtain 0.

Example 3:

Input: num = 123
Output: 12

 

Constraints:

  • 0 <= num <= 106

Approach Overview

Problem Overview: You are given an integer num. If the number is even, divide it by 2. If it is odd, subtract 1. Repeat until the value becomes 0 and return the total number of operations performed.

Approach 1: Iterative Simulation (O(log n) time, O(1) space)

The most direct approach simulates the operations exactly as described. Start with num and repeatedly apply the rule: if num % 2 == 0, divide by 2; otherwise subtract 1. Each operation reduces the magnitude of the number quickly, especially the division by 2 which removes one binary digit. Because each step either clears the least significant bit or shifts the number right, the loop runs proportional to the number of bits in num, giving O(log n) time. This approach uses constant extra memory and works well in any language.

The key insight comes from bit manipulation. In binary, subtracting 1 removes a trailing 1, and dividing by 2 shifts bits right. That means every 1 bit contributes one subtraction step, while every bit position contributes one division step except the last. This explains why the algorithm runs in logarithmic time relative to the value of num.

Approach 2: Recursive Reduction (O(log n) time, O(log n) space)

The same logic can be expressed recursively. Define a function that returns 0 when num == 0. For other values, perform one operation and call the function on the updated number. If the number is even, recurse with num / 2; otherwise recurse with num - 1. Each recursive call reduces the number toward zero, so the recursion depth equals the number of operations performed.

This version highlights the mathematical structure of the problem and maps naturally to the rules described in the prompt. However, it uses call stack space proportional to the number of steps, which is O(log n). In practice, the iterative version is usually preferred for production code because it avoids recursion overhead.

The problem primarily falls under math reasoning and bit manipulation, since the operations correspond directly to binary properties of the number.

Recommended for interviews: The iterative approach. It is simple, efficient, and demonstrates clear reasoning about how repeated division shrinks the number. Explaining the binary interpretation (each bit causing either a subtract or divide step) shows deeper understanding and often impresses interviewers.

Approach 1: Iterative Approach

In this approach, we use a loop to repetitively apply the given conditions on the number. If the number is even, divide it by 2, and if odd, subtract 1. We count and return the number of operations needed to reduce the number to zero.

The function numberOfSteps iteratively checks if the number is even or odd, performs the respective operation, and increments the step counter until the number reduces to zero. The result is printed in the main function.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(log n) where n is the initial number.
Space Complexity: O(1), since no additional space is used.

Try this approach in the editor →

Approach 2: Recursive Approach

This approach uses recursion to handle the problem. The function calls itself with the same logical conditions: divide by 2 for even or subtract 1 for odd, until the base case of zero is reached. It accumulates the count of steps through recursive calls.

This recursive solution involves the function calling itself while reducing the number based on its parity until it terminates on zero, accumulating step count in the process.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(log n)
Space Complexity: O(log n), considering function call stack for recursion.

Try this approach in the editor →

Approach 3: Default Approach

Code

Python

Java

C++

Go

TypeScript

Rust

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Iterative Approach

Time Complexity: O(log n) where n is the initial number.
Space Complexity: O(1), since no additional space is used.

Recursive Approach

Time Complexity: O(log n)
Space Complexity: O(log n), considering function call stack for recursion.

Default Approach—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Iterative SimulationO(log n)O(1)Best general solution; minimal memory and straightforward implementation
Recursive ReductionO(log n)O(log n)Useful for demonstrating the mathematical recurrence or practicing recursion

Video Solution

Number of Steps to Reduce a Number to Zero | Live Coding with Explanation | Leetcode - 1342 • Algorithms Made Easy • 3,264 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Number of Steps to Reduce a Number to Zero easy or hard?
Number of Steps to Reduce a Number to Zero is classified as an Easy problem. The logic is straightforward simulation, but recognizing the connection to binary representation helps explain why the algorithm runs in logarithmic time.
Number of Steps to Reduce a Number to Zero Python/Java solution
Both Python and Java implementations follow the same logic: loop while the number is not zero, increment a counter, divide by two if the number is even, otherwise subtract one. The algorithm runs in O(log n) time and constant space across all languages.
How to solve Number of Steps to Reduce a Number to Zero in O(log n)?
Use a loop that continues while the number is greater than zero. If the number is even, divide it by two; if it is odd, subtract one. Because each step either removes a binary digit or clears a bit, the algorithm runs in O(log n) time.
What is the best approach for Number of Steps to Reduce a Number to Zero?
The iterative simulation approach is the best choice. Repeatedly check if the number is even or odd, divide by 2 for even numbers, and subtract 1 for odd numbers. Each operation reduces the number quickly, resulting in O(log n) time complexity and O(1) extra space.
Is Number of Steps to Reduce a Number to Zero asked at Google/Amazon/Meta?
This problem is categorized as an easy algorithm question and has appeared in interview preparation sets for companies like Amazon and Google. It is commonly used to evaluate basic problem-solving skills and understanding of binary operations.
What data structure is used in Number of Steps to Reduce a Number to Zero?
No specialized data structure is required. The solution relies on arithmetic operations and simple control flow. The underlying concept relates to bit manipulation because dividing by two corresponds to shifting bits right in binary.
What is the time complexity of Number of Steps to Reduce a Number to Zero?
The time complexity is O(log n). Each division by 2 removes one binary digit from the number, and each subtraction clears a trailing 1 bit. Since the number of bits in n is proportional to log n, the total number of operations grows logarithmically.

Ready to solve this problem?

Practice Number of Steps to Reduce a Number to Zero with our built-in code editor and test cases.

Practice on FleetCode