Minimize Cash Flow among a given set of friends who have borrowed money from each other Given a number of friends who have to give or take some amount of money from one another. Design an algorithm by which the total cash flow among all the friends is minimized. Example: Above debts can be settled in following optimized way We strongly recommend to minimize your browser and try this yourself first. The idea is to use Greedy algorithm where at every step, settle all amounts of one person and recur for remaining n-1 persons. How to pick the first person? To pick the first person, calculate the net amount for every person where net amount is obtained by subtracting all debts (amounts to pay) from all credits (amounts to be paid). Once net amount for every person is evaluated, find two persons with maximum and minimum net amounts. These two persons are the most creditors and debtors. The person with minimum of two is our first person to be settled and removed from list.
Read full article from Minimize Cash Flow among a given set of friends who have borrowed money from each other - GeeksforGeeks