Start learning free

LESSON 218 OF 285 · WHILE LOOPS - CHALLENGES

Halving Steps in Go: 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

    GO · WHILE LOOPS - CHALLENGES

    Halving Steps in Go, explained

    Problem overview

    The function halvingSteps receives a positive integer n and returns how many times it must be divided by 2 until it becomes 1. Each division is an integer division, so the remainder is dropped: 9 divided by 2 is 4. Starting at 8, the path is 8, 4, 2, 1, which is 3 divisions. Starting at 9 it is 9, 4, 2, 1, also 3. If n is already 1, no division is needed and the answer is 0.

    Approach

    Repeat the division while n is bigger than 1 and count the repeats. Since the number of repeats isn't known up front, a loop with a condition fits. Go has no while, so it is written as a for with only a condition: for n > 1.

    For n = 9, the values after each pass are:

    Passn afterwardssteps
    141
    222
    313

    The loop stops as soon as n is 1, so the count holds every division made on the way.

    Step by step

    1. Create steps with :=, starting at 0. No division has been made.
    2. Loop while n > 1. If n is 1 from the start, the loop doesn't run and the answer stays 0.
    3. Divide: n = n / 2. With two integers, / drops the remainder, so 9 becomes 4. This is also why the loop ends: n gets smaller on every pass.
    4. Count it with steps++, still inside the loop.
    5. Return steps after the loop.

    The solution in Go

    func halvingSteps(n int) int {
        steps := 0
        for n > 1 {
            n = n / 2
            steps++
        }
        return steps
    }
    

    Go details:

    • Integer division. Because n is an int, n / 2 is a whole number. There is no fractional part to worry about.
    • The loop always ends. For n > 1, n / 2 is at least 1 and strictly smaller than n, so n reaches 1 and never drops to 0. That is why the condition can be n > 1.
    • Statements, not expressions. steps++ is a statement in Go, so it goes on its own line.

    Complexity

    Each pass halves n, so the loop runs about log base 2 of n times. For the largest input, 1000000000, it runs 29 times. The space is constant.

    There is a shortcut for the same number, which the lesson doesn't ask for: the count equals the position of the highest set bit of n. In a file of your own, with math/bits imported, bits.Len(uint(n)) - 1 gives 3 for 8, 3 for 9, 0 for 1 and 29 for 1000000000. The loop shows the idea of repeated halving, and the shortcut is just a faster way to get the same answer.

    Edge cases and common mistakes

    • n = 1. The answer is 0, with no pass at all.
    • Odd numbers. The remainder is dropped, so 9 goes to 4, not 4.5. The count stays correct because every pass still shrinks n.
    • Powers of 2. halvingSteps(8) is 3 because 8 is 2 multiplied by itself three times. For a power of 2 the answer is that exponent.
    • Using n > 0. The loop then goes one pass too far, from 1 to 0, and the count is off by one.
    • Counting before checking. Starting steps at 1 gives the wrong answer for n = 1.

    Testing

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