Reduce any Number to Zero in Python with Loops and Modulo

by Kal Bartal

The original problem of “1342. Number of Steps to Reduce a Number to Zero” was published on LeetCode.com

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

The Challenge

This question is asking for the number of steps it takes to reduce a given integer to 0. In one step, if the current number is even, you must divide it by 2. Otherwise, you must subtract 1 from it.

For example, if the given number is 14, the number of steps required to reduce it to 0 is 6.

Here are the steps.

  1. Divide 14 by 2, resulting in 7.
  2. Subtract 1 from 7, resulting in 6.
  3. Divide 6 by 2, resulting in 3.
  4. Subtract 1 from 3, resulting in 2.
  5. Divide 2 by 2, resulting in 1.
  6. Subtract 1 from 1, resulting in 0.

Finally, it is important to note that 0 <= num <= 106.

The Solution

To solve this problem, you need to understand the concept of a loop, and how to check if a number is even or odd. You also need to know basic mathematical operations and be able to implement them programmatically. Furthermore, you need to be able to debug and troubleshoot your code as you analyse the output.

What is the concept of a loop?

A loop is a structure in a computer program that allows a set of instructions to be executed repeatedly until a certain condition is met. It is often used to perform iterative tasks or to repeat code while a certain condition remains true.

How to check if a number is even or odd?

A number is even if it is divisible by two with no remainder, while a number is odd if it is not divisible by two. A simple way to check if a number is even or odd is to use the modulo operator (%). This operator returns the remainder of two numbers when divided. If a number, n, is even, then n % 2 will be 0 (no remainder). If a number, m, is odd, then m % 2 will be 1 (remainder of 1).

How to program basic math operations?

Basic mathematical operations include addition, subtraction, multiplication, division, powers, roots, and higher order functions. Implementing these operations programmatically involves understanding the syntax and function calls for the programming language and being comfortable with iterative looping and if statements when needed.

How to debug and troubleshoot your code?

Debugging and troubleshooting your code involves analysing the output of your program and isolating issues that arise. Standard techniques for debugging and troubleshooting include using print statements to check variables, using breakpoints to pause execution at specific lines of code, and stepping through the code line-by-line to analyse the program logic path.

Let’s develop the solution

def numberOfSteps(self, num): 
    # Initialize a counter to keep track of the steps
    counter = 0
    
    # Loop until the number is equal to zero
    while num > 0: 
        # Check if the number is even
        if num % 2 == 0: 
            # If it is even, divide it by 2
            num = num//2
            counter += 1
        # Check if the number is odd
        else: 
            # If it is odd, subtract 1 from it
            num -= 1
            counter += 1
    # Return the total number of steps
    return counter

This code takes an integer parameter, num, as its argument. It first initializes a counter variable to keep track of the number of steps required to reduce the input parameter to zero.

Next, it begins a while loop that runs until the input parameter is equal to zero. Inside the loop, we first use an if statement to check if the input parameter (num) is even. If it is even, we divide it by 2 and increment the counter variable. If the input parameter is not even, we subtract 1 from it and increment the counter variable.

Finally, the function returns the total number of steps needed to reduce the input parameter to zero.

What is the time complexity of this code?

The time complexity of this code is O(n), because the while loop runs each iteration until the input parameter is equal to zero, which means the value of the input limits the loop’s iterations. This means the algorithm takes linear time to process the input, making it O(n).

What is the space complexity of this code?

The space complexity of this code is O(1), since the only additional memory needed is for the counter variable, which is constant and does not change with the size of the input. Therefore, the space complexity is O(1).

Related Posts

Leave a Comment