Start learning free

LESSON 215 OF 285 · WHILE LOOPS - CHALLENGES

Sum Until Limit in Java: A While Loop That Stops at the Total

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

    JAVA · WHILE LOOPS - CHALLENGES

    Sum Until Limit in Java, explained

    Problem overview

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

    For limit = 10, 1 + 2 + 3 + 4 equals 10 exactly, so the answer is 4. For limit = 11, the total after 4 numbers is 10, which is still below 11, so 5 is added as well. The total becomes 15 and the answer is 5. The last number added counts even when the total passes the limit. For limit = 1 the first number is enough, so the answer is 1.

    Approach: keep a total and a counter

    Two variables describe the state: total, the sum so far, and count, how many numbers have been added. Because the numbers are 1, 2, 3, and so on, count is also the last number added, so the next number to add is the incremented count. A while loop keeps going as long as total < limit. It stops at the first moment the total reaches the limit, and count is the answer.

    Step by step

    1. Create total as an int starting at 0.
    2. Create count as an int starting at 0.
    3. Write while (total < limit). It runs as long as more numbers are needed.
    4. Increase count with count++. It is now the next number to add.
    5. Add count to total with total += count. The order of these two lines matters: count goes up first, so the first number added is 1.
    6. Return count after the loop.

    The solution in Java

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

    Tracing limit = 11:

    Passcount aftertotal afterContinue?
    111yes
    223yes
    336yes
    4410yes
    5515no

    The method returns 5. For the largest allowed limit, 1000000000, the answer is 44721, and the total at that point is a little over a billion, well inside the int range.

    Java details:

    • total += count is shorthand for total = total + count.
    • Overflow. An int tops out at 2147483647 and wraps silently. Within the lesson's limits the total never gets near that, but a limit of 2147483647 would push the total past it and the method would return the wrong number, -833855398. A long total would be needed for such limits.

    Checking the answer

    When the loop ends with count numbers added, two facts hold: the total of 1 through count is at least limit, and the total of 1 through count - 1 is still below it. For limit = 10 the answer 4 has a total of 10 and a total of 6 one step earlier. For limit = 11 the answer 5 has a total of 15, with 10 one step earlier. If both facts hold, the answer is the smallest count that reaches the limit, which is what the task asks for.

    Edge cases and common mistakes

    • Exact hit. With limit = 10 the total equals the limit, so total < limit is false and the loop stops. Using <= would add one number too many.
    • Passing the limit. The last number is added even when it overshoots, so the loop checks before adding, not after.
    • Swapped lines. If total += count came before count++, the first number added would be 0, and the count would run one ahead of the numbers actually added.
    • Returning total. The task asks for how many numbers, not their sum.

    Testing

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