MediumArrayDynamic Programming

Partition Equal Subset Sum

LeetCode
1 approach, code in all languages

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
You've got the patterns

Patterns get you through the screen. Shipping gets you hired.

FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.

750+ engineers trained · frontend, backend, AWS & AI

August 15 · 0d left
Enroll Now