← Back to problems

4. Expense Sharing

MEDIUM
ArithmeticDESIGNLLDLedger

Track shared expenses across a group and report net balances, using integer cents to avoid rounding drift.

Model a shared-expense ledger. All money amounts are integers (think cents) to avoid floating-point error. Each user has a net balance: positive means the group owes them, negative means they owe the group. Commands: - ADDUSER <name> : register a user with zero balance. - EXPENSE <payer> <amount> EQUAL <k> <u1> ... <uk> : the payer paid <amount>, split EQUALLY among the k listed users. Use floor division; distribute any remainder as 1 extra unit to the first participants in the listed order. - EXPENSE <payer> <amount> EXACT <k> <u1> <a1> ... <uk> <ak> : the payer paid <amount>, and each listed user owes exactly the given amount (the a-values sum to <amount>). - BALANCE <name> : print "<name> <net>". - SHOW : print every user with a nonzero balance as "<name> <net>", sorted ascending by name. If everyone is settled, print "SETTLED". A participant's own share is not something they owe themselves; only other participants' shares move money to the payer.
Log in to submit a solution

Comments

Log into join the discussion.