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
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We want to find the fewest operations to turn a number into zero. A brute force approach means we'll try every possible operation, one at a time, until we reach zero, keeping track of the steps.
Here's how the algorithm would work step-by-step:
def steps_to_zero_brute_force(number):
# Use a helper function to explore the possible paths.
def calculate_steps(current_number, step_count):
if current_number == 0:
return step_count
if current_number % 2 == 0:
# If even, divide by 2 and increment step count.
return calculate_steps(current_number // 2, step_count + 1)
else:
# Explore both adding and subtracting one.
subtract_one_steps = calculate_steps(current_number - 1, step_count + 1)
add_one_steps = calculate_steps(current_number + 1, step_count + 1)
# Choose the path with the minimum steps.
return min(subtract_one_steps, add_one_steps)
# Initiate the recursive calls
return calculate_steps(number, 0)
This problem asks us to figure out the fewest operations needed to reach zero. The key idea is to look at whether the number is even or odd and then decide what to do based on that. By consistently applying the correct operation, we quickly get to zero.
Here's how the algorithm would work step-by-step:
def number_of_steps_to_zero(number):
steps_count = 0
while number > 0:
# Even numbers benefit from division.
if number % 2 == 0:
number /= 2
steps_count += 1
else:
# Odd numbers require subtraction to become even.
number -= 1
steps_count += 1
return steps_count
Case | How to Handle |
---|---|
Input is zero | Return 0 immediately as no steps are needed to reduce 0 to 0. |
Input is a small positive integer (e.g., 1, 2, 3) | The algorithm should correctly calculate the steps for these basic cases. |
Input is a large positive integer | Ensure the solution does not cause integer overflow during division or subtraction, using appropriate data types. |
Input is a power of 2 (e.g., 8, 16, 32) | The algorithm should efficiently handle powers of 2 by repeatedly dividing by 2. |
Input is an odd number close to a power of 2 (e.g., 15, 31, 63) | The solution must properly alternate subtraction and division for these numbers. |
Integer overflow if steps get too large | Use a 64-bit integer type (long) to store the number of steps to avoid potential overflow issues. |
Negative Input | The prompt specifies a non-negative integer; the solution should throw an exception or return an error code if the input is negative. |
Maximum integer value | Test with the maximum integer value to make sure intermediate calculations do not overflow, using data types that can handle the calculations. |