How much room is left
A venue has a maximum capacity and a list of party sizes arriving. Return how many more guests can still enter.
- Sum all party sizes and subtract from capacity.
- If the parties already fill or exceed the room the answer is zero, never negative.
- An empty party list means nobody arrived yet.
capacityLeft(capacity: int, partySizes: list<int>) → int
C++ needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.
Where you start
int capacityLeft(int capacity, std::vector<int> partySizes) {
}
Worked examples
| Call | Result |
|---|---|
capacityLeft(100, std::vector<int>{20, 25, 30}) | 25 |
capacityLeft(50, std::vector<int>{10, 20, 25}) | 0 |
capacityLeft(100, std::vector<int>{20, 25, 30, 15, 12}) | 0 |
capacityLeft(200, std::vector<int>{}) | 200 |
Hint
Sum the list, subtract from capacity, and floor at zero.
Reference solution in C++
int capacityLeft(int capacity, std::vector<int> partySizes) {
int total = 0;
for (int s : partySizes) total += s;
return std::max(0, capacity - total);
}