Partition Equal Subset Sum: the idea to remember.
Decide whether positive integers can be divided into two subsets with equal sum.
The core insight
An even total reduces the problem to reaching half the sum using each input occurrence once.
Why it works
Every reachable sum either omits the current value or adds it to a previously reachable sum.
The mistake to avoid
An odd total is impossible. Use previous layer state so one number is not reused within the same iteration.
Commented Java solution
Use java.util.* imports and a surrounding solution class. Tree snippets assume the standard TreeNode definition; linked lists use ListNode and Clone Graph uses a Node with val and neighbors; Copy Random List uses Node with val, next and random. Read the input assumptions above.
boolean canPartition(int[] nums) {
int sum = Arrays.stream(nums).sum();
if ((sum & 1) != 0) return false;
int target = sum / 2;
boolean[] reachable = new boolean[target + 1];
reachable[0] = true;
for (int value : nums) {
// Descend so this occurrence cannot be used twice.
for (int total = target; total >= value; total--)
reachable[total] |= reachable[total - value];
}
return reachable[target];
}
Make the next connection
Independent revision notes with credited Java references. Not affiliated with LeetCode or the source sheets.