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
- Create
totalas anintstarting at 0. - Create
countas anintstarting at 0. - Write
while (total < limit). It runs as long as more numbers are needed. - Increase
countwithcount++. It is now the next number to add. - Add
counttototalwithtotal += count. The order of these two lines matters:countgoes up first, so the first number added is 1. - Return
countafter 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:
| Pass | count after | total after | Continue? |
|---|---|---|---|
| 1 | 1 | 1 | yes |
| 2 | 2 | 3 | yes |
| 3 | 3 | 6 | yes |
| 4 | 4 | 10 | yes |
| 5 | 5 | 15 | no |
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 += countis shorthand fortotal = total + count.- Overflow. An
inttops out at 2147483647 and wraps silently. Within the lesson's limits the total never gets near that, but a limit of2147483647would push the total past it and the method would return the wrong number,-833855398. Alongtotal 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 = 10the total equals the limit, sototal < limitis 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 += countcame beforecount++, 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
10gives4.11gives5.1gives1.2gives2.1000000000gives44721.
