Drill

ProblemsTypeScript › payments

Split a bill without losing a kuruş

mediumpaymentsTypeScript

A table of friends splits the bill. The app has to hand out whole minor units that add back to exactly what was charged.

splitBill(total: int, people: int) → list<int>

Solve it in the editor →

Where you start

function splitBill(total: number, people: number): number[] {
  
}

Worked examples

CallResult
splitBill(1000, 3)[334,333,333]
splitBill(1000, 4)[250,250,250,250]
splitBill(10, 4)[3,3,2,2]
splitBill(0, 3)[0,0,0]

Hint

Base share is total / people. The first (total % people) people pay one more.

Reference solution in TypeScript
function splitBill(total: number, people: number): number[] {
  if (people <= 0 || total < 0) return [];
  const base = Math.floor(total / people);
  const extra = total % people;
  const shares: number[] = [];
  for (let i = 0; i < people; i++) shares.push(i < extra ? base + 1 : base);
  return shares;
}

The same problem in another language

More payments problems in TypeScript