Greedy settlement for the Cash Flow Minimizer
Given a group of people who owe each other various amounts after splitting expenses, the naive approach is to settle every individual debt directly — if A owes B, and B owes C, that's still two separate payments, even though A could just pay C directly and reach the same end state with fewer transactions.
The Cash Flow Minimizer first nets every person down to a single balance: total owed minus total owing. Anyone with a negative balance is a net debtor; anyone positive is a net creditor. This alone collapses a tangled web of pairwise debts into a much simpler picture.
From there, the algorithm works recursively: find the person who owes the most and the person who is owed the most, settle as much of that pair as possible in one transaction, remove whichever of the two reaches zero first, and repeat on the remaining group. This greedy largest-to-largest matching is what keeps the number of transactions to a minimum.
The interesting edge cases turned out to be more about equality than logic — floating-point rounding on shared amounts, and ties where multiple people have the same balance and the tie-breaking order shouldn't matter to the final transaction count. Handling those cleanly took more care than the core algorithm itself.