Partition Equal Subset Sum (LeetCode 416) asks whether an array of non-negative integers can be divided into two groups whose sums are equal. Because the two groups must add up to the same amount, each one has to sum to exactly half of the array's total — so the question quietly turns into is there a subset that sums to half the total?, the classic subset-sum / partition problem. If the total is odd there is no whole-number half, so the answer is immediately false. You return a single boolean.
partitionEqualSubset(nums) // nums: array of non-negative integers -> boolean
partitionEqualSubset([1, 5, 11, 5]); // true — {11} and {1, 5, 5} both sum to 11
partitionEqualSubset([1, 2, 3, 5]); // false — total is 11 (odd), no equal split
partitionEqualSubset([1, 1]); // true — {1} and {1}
nums holds zero or more non-negative whole numbers; you do not need to handle negative values or fractions.total / 2, so if total is odd no split can exist. Check this before doing any real work.total / 2; whatever is left over automatically forms the matching half.true — two empty subsets both sum to 0, so [] splits (vacuously) into equal halves.The full solution is part of Premium
Walkthrough, edge cases, complexity notes, and the runnable editor unlock with a Premium subscription.
Submissions are part of Premium
Unlock community code, comments, reactions, and framework-specific approaches.
Partition Equal Subset Sum (LeetCode 416) asks whether an array of non-negative integers can be divided into two groups whose sums are equal. Because the two groups must add up to the same amount, each one has to sum to exactly half of the array's total — so the question quietly turns into is there a subset that sums to half the total?, the classic subset-sum / partition problem. If the total is odd there is no whole-number half, so the answer is immediately false. You return a single boolean.
partitionEqualSubset(nums) // nums: array of non-negative integers -> boolean
partitionEqualSubset([1, 5, 11, 5]); // true — {11} and {1, 5, 5} both sum to 11
partitionEqualSubset([1, 2, 3, 5]); // false — total is 11 (odd), no equal split
partitionEqualSubset([1, 1]); // true — {1} and {1}
nums holds zero or more non-negative whole numbers; you do not need to handle negative values or fractions.total / 2, so if total is odd no split can exist. Check this before doing any real work.total / 2; whatever is left over automatically forms the matching half.true — two empty subsets both sum to 0, so [] splits (vacuously) into equal halves.The full solution is part of Premium
Walkthrough, edge cases, complexity notes, and the runnable editor unlock with a Premium subscription.
Submissions are part of Premium
Unlock community code, comments, reactions, and framework-specific approaches.
Unlock the solution & editor
Runnable editor + tests
Solve in the browser with instant Jest feedback.
Detailed solutions
Walkthroughs, edge cases, and complexity notes.
Multi-framework variants
React, Vue, Vanilla, Angular — same question, different stacks.