Drill

ProblemsTypeScript › finance

Split a total into installments

hardfinanceTypeScript

A checkout spreads a total across installments, keeping the early ones a cent higher so each slice is as equal as possible.

installmentPlan(totalMinor: int, installments: int) → list<int>

Solve it in the editor →

Where you start

function installmentPlan(totalMinor: number, installments: number): number[] {
  
}

Worked examples

CallResult
installmentPlan(100, 3)[34,33,33]
installmentPlan(100, 6)[17,17,17,17,16,16]
installmentPlan(7, 3)[3,2,2]
installmentPlan(10, 2)[5,5]

Hint

Compute the floor share and the leftover, then hand the leftovers to the front.

Reference solution in TypeScript
function installmentPlan(totalMinor: number, installments: number): number[] {
  const result: number[] = [];
  if (installments <= 0) return result;
  const per = Math.floor(totalMinor / installments);
  const extra = totalMinor % installments;
  for (let i = 0; i < installments; i++) result.push(i < extra ? per + 1 : per);
  return result;
}

The same problem in another language

More finance problems in TypeScript