
Let there be three friends ‘A’, ‘B’, ‘C’ with debts-
• A has to pay $ 2000 to B.
• A has to pay $ 1000 to C.
• B has to pay $ 3000 to C.
• C has to pay $ 1000 to A.
Then their minimized cash flow system will be-
• A will finally pay $ 2000 to C.
• B will finally pay $ 1000 to C.
Thus, the total cash flow among them will be $ 3000.
The first line of input contains an integer ‘T’ denoting the number of test cases to run. Then the test case follows.
Then the first line of each test case contains a single integer ‘N’ denoting the number of friends.
The next ‘N’ lines contain ‘N’ space-separated integers where each element money[i][j] denotes the amount of money i-th person has to give to the j-th person.
For each test case, print an integer denoting the minimum cash flow among friends to settle their debt.
Output for each test case will be printed in a new line.
You do not need to print anything; it has already been taken care of. Just implement the given function.
1 <= T <= 50
1 <= N <= 100
1 <= MONEY[i][j] <= 10^5
Time Limit: 1sec
Let us calculate the net amount for every person by subtracting all the debts(amount to pay) from all credit(amount to be paid to him). We will find the person with the maximum and the minimum net amount. We will greedily do the transaction between them, settle their debts and repeat the same process.
The algorithm will be-
Pair Product Div by K
Pair Product Div by K
Merge Two Sorted Arrays Without Extra Space
Merge Two Sorted Arrays Without Extra Space
Co-Prime
First Digit One
Special Digit Numbers