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:
| Turn | count | total |
|---|---|---|
| 1 | 1 | 1 |
| 2 | 2 | 3 |
| 3 | 3 | 6 |
| 4 | 4 | 10 |
| 5 | 5 | 15 |
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
- Declare
totalwithletas anumber, starting at 0. It holds the sum so far. - Declare
countthe same way, starting at 0. It counts the numbers added. - Write a
whileloop with the conditiontotal < limit. It stops exactly when the adding must stop. - Move to the next number with
count++. On the first turn it becomes 1, on the second 2, and so on. - Add it:
total += count. The increment comes first, so the number added is the new one. - Return
countafter 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 += countbeforecount++, 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
numberis a double. Forlimit = 1000000000the loop turns 44721 times and the total stays far below 2^53, so every value is exact. - Types are labels. The
: numberannotations 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 = 10gives4.limit = 11gives5.limit = 1gives1.limit = 2gives2.limit = 3gives2.limit = 1000000000gives44721.
