Start learning free

LESSON 215 OF 673 · WHILE LOOPS - CHALLENGES

Halving Steps in Python: Counting Divisions by 2

Practice Read the full explanation

Given a positive integer n, return how many times you have to divide it by 2 until it becomes 1.

Every division is an integer division: the remainder is dropped, so 9 divided by 2 gives 4. Keep dividing the new value by 2 until it is 1, and count the divisions. If n is already 1, no division is needed.

 

Example 1:

Input: n = 8
Output: 3
Explanation: 8 → 4 → 2 → 1: three divisions by 2.

Example 2:

Input: n = 9
Output: 3
Explanation: 9 → 4 → 2 → 1. 9 divided by 2 gives 4, because the remainder 1 is dropped.

Example 3:

Input: n = 1
Output: 0
Explanation: n is already 1, so there is nothing to divide.

 

Constraints:

  • 1 <= n <= 109
STEP 1 OF 5
    Ctrl + Enter to check

    PYTHON · WHILE LOOPS - CHALLENGES

    Halving Steps in Python, explained

    Problem overview

    The function halvingSteps receives a positive integer n and returns how many times it must be divided by 2 before it becomes 1. Every division is an integer division, so the remainder is dropped. For n = 8 the path is 8, 4, 2, 1 and the answer is 3. For n = 9 it is 9, 4, 2, 1, because 9 divided by 2 gives 4, and the answer is again 3. If n is already 1, nothing is divided and the answer is 0.

    Approach

    The number of repetitions isn't known in advance, which makes this a job for a while loop rather than a for loop. Keep a counter, divide n by 2 while it is bigger than 1, and add one to the counter on each pass. When n reaches 1 the loop stops, and the counter holds the answer.

    The loop condition n > 1 handles the case of n = 1 on its own: the test fails at once, the body never runs, and the counter stays 0.

    Step by step

    1. Start steps at 0. No division has been made yet.
    2. Loop while n > 1. At 1 the goal is reached, so the loop stops there.
    3. Set n to n // 2. The value gets smaller on every pass, so the loop is sure to end.
    4. Add 1 to steps. Each pass of the loop is one division.
    5. Return steps after the loop.

    On n = 9 the loop runs like this:

    Passn after dividingsteps
    141
    222
    313

    Now n is 1, the condition n > 1 is false, and the function returns 3.

    The solution in Python

    class Solution:
        def halvingSteps(self, n: int) -> int:
            steps = 0
            while n > 1:
                n = n // 2
                steps += 1
            return steps
    

    Details worth knowing:

    • // against /. 9 // 2 is 4, but 9 / 2 is the float 4.5. With / the value would go 9, 4.5, 2.25, and so on, and it would never be exactly 1, so the counting would be wrong.
    • Floor division and odd numbers. // rounds down, so odd numbers lose their extra 1: 3 // 2 is 1. That is why 9 and 8 take the same number of steps.
    • The answer grows slowly. Halving shrinks the number very fast. For n = 1000000000 the function returns only 29.
    • The loop variable is the parameter. The function changes its own copy of n. The caller's number isn't affected.

    Edge cases and common mistakes

    • n equals 1. The answer is 0, with no special case needed.
    • n equals 2 or 3. Both take one step: 2 // 2 and 3 // 2 are both 1.
    • Using n > 0. The loop would divide 1 once more, to 0, and the answer would be one too large. For 8 it would count 4.
    • Forgetting to store the result. Writing n // 2 alone computes a value and throws it away, so n never shrinks and the loop never ends.

    Testing

    • 8 gives 3.
    • 9 gives 3.
    • 1 gives 0.
    • 2 gives 1.
    • 3 gives 1.
    • 1000000000 gives 29.
    Keep going with Python650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free