Start learning free

LESSON 219 OF 291 · WHILE LOOPS - CHALLENGES

Sum Until Limit in Kotlin: A while Loop That Counts

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

    KOTLIN · WHILE LOOPS - CHALLENGES

    Sum Until Limit in Kotlin, explained

    Problem overview

    The function 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 last number counts, even if it pushes the total past the limit.

    For limit = 10, 1 + 2 + 3 + 4 = 10 reaches the limit 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 too. The total becomes 15 and the answer is 5. For limit = 1 the first number already reaches it, so the answer is 1.

    Approach

    Keep two variables: total, the sum so far, and count, how many numbers have been added. Because the numbers are 1, 2, 3 in order, count is also the last number added. Each pass raises count by one and adds it to total. The loop continues while total < limit and stops the moment the total reaches the limit.

    The values after each pass for limit = 11:

    Passcount aftertotal after
    111
    223
    336
    4410
    5515

    After pass 4 the total, 10, is below 11, so the loop runs again. After pass 5 it is 15, the check fails, and the function returns 5.

    Step by step

    1. Create total with var, starting at 0. It changes inside the loop, so val would not compile.
    2. Create count with var, starting at 0. No number has been added yet.
    3. Loop while total < limit. The loop ends when the total reaches or passes the limit, which is when the adding must stop.
    4. Move to the next number: count++. It is 1 on the first pass, 2 on the second.
    5. Add it to the total: total += count. The order matters: count goes up first, so the number added is the next one in the sequence.
    6. Return count.

    The solution in Kotlin

    class Solution {
        fun sumUntilLimit(limit: Int): Int {
            var total = 0
            var count = 0
            while (total < limit) {
                count++
                total += count
            }
            return count
        }
    }
    

    Some details worth knowing:

    • The check happens before each pass. That is why the number that reaches the limit is still added: the loop only notices after the pass is done.
    • No Int overflow. With the largest limit, 1000000000, the loop ends after 44721 passes. The total stays near the limit, far below the Int maximum of 2147483647.
    • += and ++. total += count means total = total + count, and count++ adds 1 to count.

    Complexity

    The total after k numbers is about k²/2, so the loop runs roughly the square root of 2 * limit times. Time is O(√limit), and the largest input takes 44721 passes. Space is O(1).

    Edge cases and common mistakes

    • Stopping one number early. With total <= limit as the condition, the loop would continue after reaching the limit exactly, and limit = 10 would give 5.
    • Swapping the two lines. If total += count runs before count++, the first number added is 0 and the answer is off by one.
    • A limit of 1. The first pass reaches it, so the answer is 1, not 0.
    • Returning total. The task asks how many numbers were added, not their sum.

    Testing

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