MediumArrayDynamic Programming

1 approach, code in every language

Problem description

You are given an array of positive integers. Determine whether the array can be divided into two subsets such that the sum of the elements in both subsets is exactly the same.

Every element must belong to exactly one of the two subsets, and neither subset may be empty in a valid equal split (though the standard framing simply asks whether such a balanced split exists).

Return true if such a partition is possible, and false otherwise.

Example 1

Input: nums = [1,5,11,5]

Output: true

The array can be split into {1,5,5} and {11}, both summing to 11, so an equal partition exists.

Example 2

Input: nums = [1,2,3,5]

Output: false

The total is 11, which is odd, so it can never be divided into two equal integer halves.

Constraints

  • 1 <= nums.length <= 200
  • 1 <= nums[i] <= 100
  • The total sum is at most 200 * 100 = 2 * 10^4

After the pattern

Now solve one you haven't seen.

Even Split of Crate Loads is our own problem on the technique behind Partition Equal Subset Sum: a new statement, so there's no answer to remember. Write it in your browser and run it against graded tests.

1,194 problems · 15,062 graded tests · free