Start learning free

LESSON 217 OF 289 · WHILE LOOPS - CHALLENGES

Halving Steps in C#: A while Loop with Integer Division

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

    C# · WHILE LOOPS - CHALLENGES

    Halving Steps in C#, explained

    Problem overview

    HalvingSteps receives a positive int n and returns how many times you must divide it by 2 until it becomes 1. Each division is an integer division, so the remainder is dropped. For 8 the path is 8, 4, 2, 1: three divisions. For 9 it is 9, 4, 2, 1, also three, since 9 divided by 2 gives 4. For 1 no division is needed, so the answer is 0.

    Approach

    You don't know in advance how many divisions it takes, so a while loop fits better than a for loop. The loop keeps going while n is bigger than 1. Each pass divides n by 2 and counts one step. A separate counter, steps, remembers how many passes ran.

    In C#, dividing two int values with / truncates, so 9 / 2 is 4 and not 4.5. That matches the lesson's rule that the remainder is dropped.

    Step by step

    1. Create steps as an int set to 0. No division has happened yet.
    2. Write while (n > 1). When n is 1 from the start, the loop doesn't run and the answer stays 0.
    3. Divide n by 2 and store the result back in n. It gets smaller on every pass, so it must reach 1.
    4. Add 1 to steps with ++. Each pass is one division.
    5. Return steps after the loop.

    For n = 9:

    Passn after dividingsteps
    141
    222
    313

    Now n > 1 is false, so the loop ends and the method returns 3.

    The solution in C#

    class Solution {
        public int HalvingSteps(int n) {
            int steps = 0;
            while (n > 1) {
                n = n / 2;
                steps++;
            }
            return steps;
        }
    }

    The method changes its own copy of n, which is fine, because the caller's number is untouched.

    Division in C#

    The kind of division depends on the operand types, not on how you want the answer. With int a = 7, b = 2, a / b is 3, a % b is 1, and (double)a / b is 3.5. In this lesson both operands are integers, so you get the whole-number result for free, and the remainder is thrown away.

    Written as a for loop, the same idea is for (; n > 1; n /= 2) steps++;. A while loop shows the three parts, the test, the division and the count, more clearly.

    Two more checks on the answer: a power of two such as 8 gives exactly its exponent, 3, and the number just below the next power, such as 15, gives the same count as 8 because every division rounds down.

    Complexity

    Each pass halves n, so the loop runs about log base 2 of n times. That is the answer itself. For n = 1000000000, the largest allowed input, it is 29 passes. Memory use is constant: two integers.

    Edge cases and common mistakes

    • n = 1. The loop is skipped and the answer is 0.
    • The wrong condition. With while (n > 0), the loop would go on to 0 and count one step too many.
    • A missing division. If n is never changed, the loop never ends.
    • Odd numbers. The remainder is dropped, so 9 goes to 4 and 5 goes to 2. Odd numbers need no special handling.

    Testing

    The solution returns 3 for 8, 3 for 9, 0 for 1 and 29 for 1000000000.

    Keep going with C#1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free