Maintained by Vikas Dulgunde, software engineer
You receive an array of positive integers. The goal is to tell whether the numbers can be divided into two groups so that both groups add up to the same total. Every number must land in exactly one of the two groups.
Because both halves must be equal, each one has to add up to exactly half of the whole array's sum. That immediately tells you something useful: if the total sum is odd, no split is possible, since half of an odd number is not a whole value that integer subsets can reach.
When the total is even, the question reduces to a single yes-or-no decision: can you pick a subset of the numbers that adds up to exactly half the total? If such a subset exists, the leftover numbers form the matching half automatically, so you only ever need to hunt for one of the two groups.
Return true if such an even split exists and false if it does not.
The total is 22, so each half must reach 11. The subset [1, 5, 5] sums to 11 and the remaining [11] also sums to 11.
The total is 11, an odd number, so no two subsets can ever tie. The answer is false without checking any combination.
The total is 18, so each half needs 9. Picking [4, 5] reaches 9 and the three 3s on the other side also reach 9.
Visible test cases
First check the total sum. If it is odd, you can stop right away and return false.
When the total is even, you no longer need both groups. Ask a simpler question: is there a subset that adds up to half the total?
Track every subset sum you can build as you walk through the numbers. A boolean array indexed by sum, updated one number at a time, answers the subset question without listing actual subsets.
Try every possible subset and see if any one reaches half of the total sum.
Treat it as a 0/1 knapsack: track which sums up to target are reachable using a one-dimensional boolean table, updated right to left so each number is used at most once.