How much is still owed
Compare the amount due against a list of payments already received and return the shortfall.
- Shortfall is due minus total payments, clamped at zero.
- An empty payment list means the full amount is still owed.
balanceShortfall(due: int, payments: list<int>) → int
Java 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 balanceShortfall(int due, List<Integer> payments) {
}
Worked examples
| Call | Result |
|---|---|
balanceShortfall(10000, Main.<Integer>ls(3000, 4000)) | 3000 |
balanceShortfall(5000, Main.<Integer>ls(5000)) | 0 |
balanceShortfall(5000, Main.<Integer>ls(6000)) | 0 |
balanceShortfall(1000, Main.<Integer>ls()) | 1000 |
Hint
Sum the payments and subtract from due.
Reference solution in Java
int balanceShortfall(int due, List<Integer> payments) {
int paid = 0;
for (int p : payments) paid += p;
return Math.max(0, due - paid);
}