Start learning free

LESSON 219 OF 289 · WHILE LOOPS - CHALLENGES

Sum Until Limit in TypeScript: A while Loop with Two Counters

Practice Read the full explanation

Given a positive integer limit, add up the numbers 1, 2, 3, … in this order, and stop as soon as the total is at least limit. Return how many numbers you added.

The total may go past limit: the number that makes it reach or pass limit is the last one added, and it counts.

 

Example 1:

Input: limit = 10
Output: 4
Explanation: 1 + 2 + 3 + 4 = 10 reaches the limit exactly, so 4 numbers were added.

Example 2:

Input: limit = 11
Output: 5
Explanation: 1 + 2 + 3 + 4 = 10 is still less than 11, so 5 is added too. The total becomes 15, and 5 numbers were added.

Example 3:

Input: limit = 1
Output: 1
Explanation: The first number, 1, already reaches the limit.

 

Constraints:

  • 1 <= limit <= 109
STEP 1 OF 6
    Ctrl + Enter to check

    TYPESCRIPT · WHILE LOOPS - CHALLENGES

    Sum Until Limit in TypeScript, explained

    Problem overview

    The method sumUntilLimit receives a positive integer limit. It adds the numbers 1, 2, 3, and so on, in that order, and stops as soon as the total is at least limit. It returns how many numbers it added. The total may overshoot the limit: the number that reaches or passes it is the last one added, and it counts.

    For limit = 10, 1 + 2 + 3 + 4 is exactly 10, so the answer is 4. For limit = 11, the sum after 4 numbers is 10, which is still below 11, so 5 is added too and the total becomes 15. The answer is 5. For limit = 1 the first number is enough and the answer is 1.

    Approach

    Two values change as the loop runs: the running total, and count, the number of terms added. Because the terms are 1, 2, 3 in order, count is also the last number added. So there is no need for a third variable: increase count to get the next number, then add it to total.

    The loop keeps going while total < limit. The test is done before each turn, so as soon as a turn makes the total reach or pass the limit, the next check fails and the loop ends.

    The trace for limit = 11:

    Turncounttotal
    111
    223
    336
    4410
    5515

    After turn 4 the total, 10, is below 11, so the loop turns once more. After turn 5 the total is 15, the check fails, and the function returns 5.

    Step by step

    1. Declare total with let as a number, starting at 0. It holds the sum so far.
    2. Declare count the same way, starting at 0. It counts the numbers added.
    3. Write a while loop with the condition total < limit. It stops exactly when the adding must stop.
    4. Move to the next number with count++. On the first turn it becomes 1, on the second 2, and so on.
    5. Add it: total += count. The increment comes first, so the number added is the new one.
    6. Return count after the loop.

    The solution in TypeScript

    class Solution {
        sumUntilLimit(limit: number): number {
            let total: number = 0;
            let count: number = 0;
            while (total < limit) {
                count++;
                total += count;
            }
            return count;
        }
    }
    
    • The order inside the loop matters. With total += count before count++, the first number added would be 0 and the loop would add one number too few. The code above increments first.
    • < and not <=. The task stops when the total is at least the limit, so the loop continues only while it is strictly below.
    • No overflow. A TypeScript number is a double. For limit = 1000000000 the loop turns 44721 times and the total stays far below 2^53, so every value is exact.
    • Types are labels. The : number annotations are erased when the code is compiled to JavaScript.

    Complexity

    Time: O(sqrt(limit)). After k turns the total is k * (k + 1) / 2, so the loop stops when k is about the square root of 2 * limit. For the largest limit that is 44721 turns.

    Space: O(1). Two numbers are kept.

    Edge cases and common mistakes

    • Limit of 1. The check passes once (0 < 1), the first number is added, and the function returns 1.
    • Exact hit. For 10 the total equals the limit after 4 numbers, and the answer is 4, not 5.
    • Overshoot. For 11 the answer is 5, although the total is 15: the number that passes the limit counts.
    • Returning total. The task asks for how many numbers were added, not for their sum.
    • Returning count - 1. That would drop the number that reached the limit, which the task counts.

    Testing

    • limit = 10 gives 4.
    • limit = 11 gives 5.
    • limit = 1 gives 1.
    • limit = 2 gives 2.
    • limit = 3 gives 2.
    • limit = 1000000000 gives 44721.
    Keep going with TypeScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free