Split a total into installments
A checkout spreads a total across installments, keeping the early ones a cent higher so each slice is as equal as possible.
- No installment differs from another by more than one minor unit.
- The first (total mod installments) installments carry the larger value; the rest the smaller.
- installments of zero or less gives an empty list.
InstallmentPlan(totalMinor: int, installments: int) → list<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
public List<int> InstallmentPlan(int totalMinor, int installments) {
}
Worked examples
| Call | Result |
|---|---|
InstallmentPlan(100, 3) | new List<int> { 34, 33, 33 } |
InstallmentPlan(100, 6) | new List<int> { 17, 17, 17, 17, 16, 16 } |
InstallmentPlan(7, 3) | new List<int> { 3, 2, 2 } |
InstallmentPlan(10, 2) | new List<int> { 5, 5 } |
Hint
Compute the floor share and the leftover, then hand the leftovers to the front.
Reference solution in C#
public List<int> InstallmentPlan(int totalMinor, int installments) {
var result = new List<int>();
if (installments <= 0) return result;
int per = totalMinor / installments;
int extra = totalMinor % installments;
for (int i = 0; i < installments; i++) result.Add(i < extra ? per + 1 : per);
return result;
}