Algorithm to Divide a list of numbers into 2 equal sum lists
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Splitting a list into two groups with the same total is the classic partition problem. The question sounds simple, but the search space grows quickly because every element can end up on either side. For realistic inputs, the usual exact solution is dynamic programming, which avoids recomputing the same subset sums over and over.
Start with the Necessary Condition
If the sum of all numbers is odd, the problem has no solution. Two equal groups must each sum to half of the total, so the first check is trivial and saves a lot of wasted computation.
For example, with [1, 5, 11, 5]:
- total sum is
22 - target for each subset is
11
With [1, 2, 4]:
- total sum is
7 - equal partition is impossible immediately
That means every algorithm should begin with:
Why Brute Force Does Not Scale
A brute-force solution tries every possible way to assign each number to the left subset or right subset. For n numbers, that is essentially 2^n possibilities. It works for tiny inputs but becomes impractical fast.
A simple recursive sketch is easy to understand:
This expresses the logic clearly, but it recomputes the same states repeatedly. That is why dynamic programming is the usual answer.
Dynamic Programming for Feasible Inputs
The core idea is to ask: which sums can I build after processing each number? If we can build target = total // 2, then the remaining numbers automatically form the other subset.
One efficient approach uses a boolean DP array:
The reverse loop is important. It prevents one number from being reused multiple times in the same iteration.
Reconstructing the Actual Two Lists
The question often asks for the two lists, not just a boolean answer. To do that, store enough information to reconstruct which elements contributed to the target.
Here is a version that returns the two groups:
Possible output is:
or another valid partition with the same total. The exact groups may differ, and that is fine as long as their sums match.
Complexity Tradeoffs
The boolean DP solution runs in O(n * target) time. That is pseudo-polynomial, not truly polynomial, because the running time depends on the numeric sum rather than just the number of elements.
This means it is practical when values are modest, expensive when sums are very large, and still much better than brute force for many real inputs.
That distinction matters in interviews and production code alike. A solution can be dramatically faster than brute force and still struggle if the numbers themselves are huge.
Handling Negative Numbers
Many textbook solutions assume non-negative integers. Once negative numbers are allowed, the usual DP table indexed by sum becomes trickier because reachable sums can be negative too. In that case you typically switch to an offset-based table or a set of reachable sums. For most interview-style versions of the problem, assume non-negative integers unless the statement says otherwise.
Common Pitfalls
- Forgetting the odd-sum check and doing expensive work for an impossible input.
- Iterating the DP array forward instead of backward, which allows the same number to be reused incorrectly.
- Returning only
TrueorFalsewhen the caller actually needs the two subsets themselves. - Assuming the problem stays easy for large values, even though runtime depends on the target sum.
- Applying the non-negative DP approach directly to inputs that contain negative numbers.
Summary
- The equal-partition problem asks whether the list can be split into two groups with the same sum.
- If the total sum is odd, the answer is immediately
False. - Dynamic programming is the standard exact approach for practical input sizes.
- Reconstructing the subsets requires storing path information, not just reachability.
- The best algorithm depends on whether you need an exact split, a boolean answer, or support for large or negative values.

