Start learning free

LESSON 218 OF 289 · WHILE LOOPS - CHALLENGES

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

    TYPESCRIPT · WHILE LOOPS - CHALLENGES

    Halving Steps in TypeScript, explained

    Problem overview

    The method 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: the remainder is dropped, so 9 divided by 2 is 4.

    For n = 8 the values are 8, 4, 2, 1, which is three divisions. For n = 9 they are 9, 4, 2, 1, again three. For n = 1 nothing has to be done, so the answer is 0.

    Approach

    Keep a counter and a loop. While n is bigger than 1, halve it and add one to the counter. When n reaches 1 the loop ends, and the counter holds the number of divisions made.

    In TypeScript, 9 / 2 is 4.5: the / operator divides doubles and does not drop anything. To get the integer division the task describes, round down with Math.floor. For positive numbers Math.trunc gives the same result.

    The trace for 9:

    Turnn after halvingsteps
    141
    222
    313

    The loop stops because n is now 1, and the function returns 3.

    Step by step

    1. Declare steps with let as a number, starting at 0. It counts the divisions and grows in the loop, so it isn't a const.
    2. Write a while loop with the condition n > 1. If n is 1 from the start, the loop doesn't run and the answer stays 0.
    3. Halve n with n = Math.floor(n / 2).
    4. Count it with steps++. Each turn is exactly one division.
    5. Return steps after the loop.

    The solution in TypeScript

    class Solution {
        halvingSteps(n: number): number {
            let steps: number = 0;
            while (n > 1) {
                n = Math.floor(n / 2);
                steps++;
            }
            return steps;
        }
    }
    
    • steps++ is a statement here. It adds 1 to steps. Its value is not used.
    • The loop always ends. For n at least 2, Math.floor(n / 2) is smaller than n and still at least 1, so n shrinks until it is 1 and never skips past it.
    • Types are labels. number is erased when TypeScript compiles to JavaScript, and it doesn't make n an integer. The Math.floor call is what keeps it whole.

    Complexity

    Time: O(log n). Each turn halves n, so the number of turns is about log2(n). The largest allowed input, 1000000000, returns 29.

    Space: O(1). Only steps and n are kept.

    Edge cases and common mistakes

    • n = 1. No turn happens and the function returns 0.
    • n = 2 and n = 3. Both give 1: 2 becomes 1, and 3 becomes 1 because 3 / 2 is 1.5, rounded down.
    • Dropping Math.floor. 9 / 2 would stay 4.5, then 2.25, 1.125, 0.5625. The condition n > 1 would still fail at 0.5625, but the count would be 4 instead of 3, so the answer would be wrong.
    • Looping while n > 0. The task stops at 1, not at 0. This adds an extra step.
    • Counting before the loop condition fails. Adding 1 at the end, outside the loop, gives an answer one too high.

    Testing

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