Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Build confidence with an intuition-first walkthrough focused on math fundamentals.
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 <= 106Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math · Bit Manipulation
14
8
123
minimum-moves-to-reach-target-score)count-operations-to-obtain-zero)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1342: Number of Steps to Reduce a Number to Zero
class Solution {
public int numberOfSteps(int num) {
int ans = 0;
while (num != 0) {
num = (num & 1) == 1 ? num - 1 : num >> 1;
++ans;
}
return ans;
}
}
// Accepted solution for LeetCode #1342: Number of Steps to Reduce a Number to Zero
func numberOfSteps(num int) int {
ans := 0
for num != 0 {
if (num & 1) == 1 {
num--
} else {
num >>= 1
}
ans++
}
return ans
}
# Accepted solution for LeetCode #1342: Number of Steps to Reduce a Number to Zero
class Solution:
def numberOfSteps(self, num: int) -> int:
ans = 0
while num:
if num & 1:
num -= 1
else:
num >>= 1
ans += 1
return ans
// Accepted solution for LeetCode #1342: Number of Steps to Reduce a Number to Zero
impl Solution {
pub fn number_of_steps(mut num: i32) -> i32 {
let mut count = 0;
while num != 0 {
if num % 2 == 0 {
num >>= 1;
} else {
num -= 1;
}
count += 1;
}
count
}
}
// Accepted solution for LeetCode #1342: Number of Steps to Reduce a Number to Zero
function numberOfSteps(num: number): number {
let ans = 0;
while (num) {
num = num & 1 ? num - 1 : num >>> 1;
ans++;
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Sort the array in O(n log n), then scan for the missing or unique element by comparing adjacent pairs. Sorting requires O(n) auxiliary space (or O(1) with in-place sort but O(n log n) time remains). The sort step dominates.
Bitwise operations (AND, OR, XOR, shifts) are O(1) per operation on fixed-width integers. A single pass through the input with bit operations gives O(n) time. The key insight: XOR of a number with itself is 0, which eliminates duplicates without extra space.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.