Start learning free

LESSON 215 OF 667 · WHILE LOOPS - CHALLENGES

Sum Until Limit in C++: 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

    C++ · WHILE LOOPS - CHALLENGES

    Sum Until Limit in C++, explained

    Problem overview

    The function sumUntilLimit receives a positive integer limit, up to 1,000,000,000. It adds the numbers 1, 2, 3 and so on, in this order, and stops as soon as the total is at least limit. It returns how many numbers it added.

    The total may go past the limit. The number that makes it reach or pass the limit is the last one added, and it counts. For limit = 10, the sum 1 + 2 + 3 + 4 is exactly 10, so the answer is 4. For limit = 11, 1 + 2 + 3 + 4 is 10, still too small, so 5 is added too, the total becomes 15 and the answer is 5. For limit = 1, the first number already reaches the limit and the answer is 1.

    Approach

    The loop needs two variables: total for the sum so far, and count for how many numbers have been added. Because the numbers go 1, 2, 3 in order, count is also the last number added. After adding 1, 2 and 3, it is 3. That means the next number to add is always count + 1, and increasing count first gives the number to add.

    The condition keeps looping while total < limit. It becomes false exactly when the total reaches or passes the limit, which is when the adding must stop. Here is the loop for limit = 11:

    count=1 total=1
    count=2 total=3
    count=3 total=6
    count=4 total=10
    count=5 total=15
    

    Step by step

    1. Create total as an int starting at 0. Nothing has been added.
    2. Create count as an int starting at 0. No numbers have been added yet.
    3. Start a while loop with the condition total < limit. With a positive limit, the loop always runs at least once.
    4. Add 1 to count with ++. It is now the next number to add: 1 on the first pass, 2 on the second.
    5. Add count to total. This must come after the increment, so the number added is the new one.
    6. Return count after the loop.

    The solution in C++

    class Solution {
    public:
        int sumUntilLimit(int limit) {
            int total = 0;
            int count = 0;
            while (total < limit) {
                count++;
                total += count;
            }
            return count;
        }
    };
    

    Details to know:

    • The total can pass the limit. For limit = 11 it ends at 15. Since total can exceed limit by at most the last number added, it stays far below the 32-bit limit of about 2.1 billion: for limit = 1000000000 the function adds 44721 numbers, and the total ends just above 1,000,000,000, by less than the last number added. Signed overflow is undefined behaviour in C++, so it is worth checking that an int is large enough, and here it is.
    • total += count is shorthand for total = total + count.
    • Only count is returned. The total is just the loop's way of knowing when to stop.

    Complexity

    Time: O(sqrt(limit)). After k numbers the total is k(k + 1) / 2, which grows like k squared, so the loop needs about the square root of 2 times limit passes. For 1000000000 that is 44721 passes. Space: O(1).

    Edge cases and common mistakes

    • limit = 1. One pass: count is 1, total is 1, and the loop ends. The answer is 1.
    • Exact hit versus overshoot. For 10 the total is exactly 10 after 4 numbers, and for 11 it overshoots to 15 after 5. The comparison total < limit handles both, because an exact hit also ends the loop.
    • Using total <= limit. That adds one extra number when the total hits the limit exactly, so 10 would return 5.
    • Adding before incrementing. If total += count comes before count++, the first number added is 0 and every addition is one too small.

    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 C++650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free