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
- Create
totalas anintstarting at 0. Nothing has been added. - Create
countas anintstarting at 0. No numbers have been added yet. - Start a
whileloop with the conditiontotal < limit. With a positive limit, the loop always runs at least once. - Add 1 to
countwith++. It is now the next number to add: 1 on the first pass, 2 on the second. - Add
counttototal. This must come after the increment, so the number added is the new one. - Return
countafter 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 = 11it ends at 15. Sincetotalcan exceedlimitby at most the last number added, it stays far below the 32-bit limit of about 2.1 billion: forlimit = 1000000000the 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 anintis large enough, and here it is. total += countis shorthand fortotal = total + count.- Only
countis 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:countis 1,totalis 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 < limithandles 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 += countcomes beforecount++, the first number added is 0 and every addition is one too small.
Testing
limit = 10gives 4.limit = 11gives 5.limit = 1gives 1.limit = 2gives 2.limit = 3gives 2.limit = 1000000000gives 44721.
