Skip to main content

Optimal Account Balancing - Solution & Explanation

HardPremiumFree on FleetCodeArrayDynamic ProgrammingBacktrackingBit Manipulation6 min readAsked at: Amazon, Microsoft, Goldman Sachs +9
Practice this problem

Problem Statement

You are given an array of transactions transactions where transactions[i] = [fromi, toi, amounti] indicates that the person with ID = fromi gave amounti $ to the person with ID = toi.

Return the minimum number of transactions required to settle the debt.

 

Example 1:

Input: transactions = [[0,1,10],[2,0,5]]
Output: 2
Explanation:
Person #0 gave person #1 $10.
Person #2 gave person #0 $5.
Two transactions are needed. One way to settle the debt is person #1 pays person #0 and #2 $5 each.

Example 2:

Input: transactions = [[0,1,10],[1,0,1],[1,2,5],[2,0,5]]
Output: 1
Explanation:
Person #0 gave person #1 $10.
Person #1 gave person #0 $1.
Person #1 gave person #2 $5.
Person #2 gave person #0 $5.
Therefore, person #1 only need to give person #0 $4, and all debt is settled.

 

Constraints:

  • 1 <= transactions.length <= 8
  • transactions[i].length == 3
  • 0 <= fromi, toi < 12
  • fromi != toi
  • 1 <= amounti <= 100

Approach Overview

Problem Overview: You receive a list of transactions between people. Each transaction transfers money from one person to another. The goal is to settle all debts with the minimum number of additional transactions.

The key observation: instead of working with the original transactions, compute each person's net balance. Positive means they should receive money, negative means they owe money. The problem then becomes pairing these balances so that all accounts end at zero using the fewest transfers.

Approach 1: Backtracking on Net Balances (O(n!) time, O(n) space)

First build a balance array using an array. Iterate through transactions and update each person's net balance. Filter out zeros because they are already settled. Then apply backtracking: pick the first non‑zero balance and try settling it with every opposite‑signed balance that appears later in the list. For each pairing, transfer the amount, recurse, and restore the balance (classic DFS with backtracking). Pruning happens when identical balances appear or when a perfect cancellation occurs. This drastically reduces the search space and works well for the typical constraint of ≤12 people with non‑zero balances.

Approach 2: Bitmask Dynamic Programming (O(n * 2^n) time, O(2^n) space)

Another approach uses dynamic programming with bit manipulation. Each bitmask represents a subset of people whose balances we try to settle internally. Compute the total balance of each subset; if it equals zero, that subset can settle with size - 1 transactions. Use DP to combine smaller zero-sum subsets into larger ones and minimize transactions. The state dp[mask] stores the minimum transactions needed for that subset. Iterating over submasks allows merging valid groups efficiently.

The DP formulation transforms the problem into partitioning people into zero-sum groups. Each group requires exactly k - 1 transactions if it contains k people.

Recommended for interviews: Backtracking with balance reduction is the most common interview solution. It shows you understand debt compression and recursive search with pruning. Bitmask DP is more advanced and demonstrates strong skills with subset states and optimization, but many candidates default to the backtracking approach because it is easier to reason about during whiteboard interviews.

Solution

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Backtracking on Net BalancesO(n!) worst caseO(n)Best practical solution for small number of participants (≤12). Common interview approach with pruning.
Bitmask Dynamic ProgrammingO(n * 2^n)O(2^n)Useful when modeling subset partitions and zero-sum groups. Demonstrates strong DP and bitmask reasoning.

Video Solution

22. Splitwise Simplify Debt Algorithm | LLD of Splitwise | Optimal Account Balancing | LLD SplitwiseConcept && Coding - by Shrayansh61,465 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Optimal Account Balancing easy or hard?
Optimal Account Balancing is classified as a Hard problem on LeetCode. The difficulty comes from recognizing that the original transactions should be compressed into net balances and then solved using backtracking or subset dynamic programming.
Optimal Account Balancing Python/Java solution
Most implementations compute net balances using a map or array, filter out zeros, and apply recursive backtracking to settle debts between opposite balances. The same logic works across Python, Java, C++, Go, and TypeScript with only minor syntax differences.
How to solve Optimal Account Balancing in O(n)?
An O(n) algorithm does not exist for the general case because the problem requires exploring combinations of debt settlements. The best practical solutions are exponential: backtracking with pruning or dynamic programming with bitmasks. Both rely on reducing the problem to a small set of net balances.
What is the best approach for Optimal Account Balancing?
Backtracking on compressed net balances is the most common solution. First compute each person's net balance, then recursively settle debts between opposite-signed balances while minimizing the number of transactions. With pruning and skipping duplicates, this approach performs well for the small constraint size and is widely used in interviews.
Is Optimal Account Balancing asked at Google/Amazon/Meta?
Optimal Account Balancing appears in advanced algorithm interview preparation and has been reported in interviews at large tech companies. The problem tests backtracking, subset reasoning, and optimization techniques, which are common themes in Google and Meta style interviews.
What data structure is used in Optimal Account Balancing?
The main data structure is an array or list storing each person's net balance after processing all transactions. The backtracking approach operates directly on this array, while the dynamic programming variant uses bitmasks to represent subsets of balances.
What is the time complexity of Optimal Account Balancing?
The backtracking solution has O(n!) worst-case time complexity where n is the number of people with non-zero balances. In practice the search space is heavily pruned. A bitmask dynamic programming alternative runs in O(n * 2^n) time with O(2^n) space.

Ready to solve this problem?

Practice Optimal Account Balancing with our built-in code editor and test cases.

Practice on FleetCode