Start learning free

LESSON 218 OF 291 · WHILE LOOPS - CHALLENGES

Halving Steps in Kotlin: 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 6
    Ctrl + Enter to check

    KOTLIN · WHILE LOOPS - CHALLENGES

    Halving Steps in Kotlin, explained

    Problem overview

    The function halvingSteps receives a positive integer n and returns how many times it must be divided by 2 to reach 1. Each division is an integer division that drops the remainder. For 8 the chain is 8, 4, 2, 1: three divisions. For 9 it is 9, 4, 2, 1, also three, because 9 divided by 2 gives 4. For 1 no division is needed, so the answer is 0.

    Approach

    Do exactly what the task says: keep dividing and keep counting. A while loop fits because you don't know in advance how many divisions it takes. The loop condition is "the number is still bigger than 1".

    The values after each pass for 9:

    Passnum aftersteps after
    141
    222
    313

    The number halves each time, so it shrinks fast: even the largest input, 1000000000, needs only 29 divisions.

    Step by step

    1. Copy n into num with var. A parameter can't be reassigned.
    2. Start steps at 0 with var, since it grows inside the loop.
    3. Loop while num > 1. For n = 1 the loop doesn't run and the answer stays 0.
    4. Divide: num = num / 2.
    5. Count the division with steps++.
    6. Return steps.

    The solution in Kotlin

    class Solution {
        fun halvingSteps(n: Int): Int {
            var num = n
            var steps = 0
            while (num > 1) {
                num = num / 2
                steps++
            }
            return steps
        }
    }
    

    Some details worth knowing:

    • Integer division. Both operands are Int, so 9 / 2 is 4, not 4.5. That is the behaviour the task asks for.
    • Why the loop ends. For num > 1, num / 2 is smaller than num and at least 1, so the value reaches 1 and stops. The condition num > 0 would be a bug: from 1, 1 / 2 is 0, and the count would be one too high.
    • The answer is the floor of log base 2. The count equals the position of the highest 1 bit of n. That is why 8 and 9 both give 3.

    Why a loop is the right tool

    The number of divisions depends on n, so the code can't be written as a fixed number of statements. A while loop with the condition from the task, "until it becomes 1", expresses that directly. The loop body does one division and one count, and the condition is checked again before the next pass.

    It helps to read the loop as a contract. Before every check, steps is the number of divisions made so far and num is what is left. When num reaches 1, nothing is left to divide, and steps is the answer. Because the check comes first, a starting value of 1 ends the function with 0 divisions and no special case.

    Complexity

    Time is O(log n), since the number halves each pass. Space is O(1).

    Edge cases and common mistakes

    • n = 1. No pass runs; the answer is 0.
    • Powers of 2. 8 gives 3, an exact count. 1000000000, which is not a power of 2, gives 29.
    • Looping to 0. Use num > 1, not num > 0.
    • Forgetting steps++. The function would always return 0.

    Testing

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