All coding questions

Partition Equal Subset Sum

medium
NeetCode 150dynamic-programmingknapsack

Problem

Given a list of positive integers, decide whether it can be split into two groups whose sums are equal.

Examples

Input: nums = [1, 5, 11, 5]
Output: true
[1, 5, 5] and [11] both sum to 11.
Input: nums = [1, 2, 3, 5]
Output: false
The total 11 is odd, so no equal split exists.

Constraints

  • 1 <= nums.length <= 200
  • 1 <= nums[i] <= 100
Hints(tap to reveal)
  • 1. An odd total can never split evenly.
  • 2. Reduce to a subset-sum targeting half the total.
Optimal approach(spoiler)
  1. Compute the total; if it is odd, return false.
  2. Target half the total and run a boolean subset-sum DP.
  3. Track which sums are reachable using a one-dimensional set or bitset.
  4. Return whether the half target is reachable.
  5. O(n * sum) time, O(sum) space.

Ready to solve it?

Write your solution in the editor and get an instant AI grade on correctness, edge cases, and complexity.

Practice in the editor