Facebook Pixel

Partition Equal Subset Sum

Given a non-empty array of non-negative integers, determine if it can be partitioned into two subsets such that the sum of elements in both subsets is equal.

Every element must land in exactly one of the two subsets; neither subset may be skipped over or reused.

Input & Output
Input
nums — the array to split
Output
whether the array can be partitioned into two subsets with equal sums
Example
Input
nums = [3, 4, 7]
Output
true
Explanation

The total is 14, so each side must sum to 7. Putting [3, 4] on one side and [7] on the other gives 7 and 7.

Example
Input
nums = [1, 2, 3, 5]
Output
false
Explanation

The total is 11. An odd total cannot be halved into two whole-number sums, so no partition exists regardless of which elements are chosen.

Constraints
  • 1 <= nums.length <= 200
  • 0 <= nums[i] <= 100

Try it yourself

Invest in Yourself
Your new job is waiting. 83% of people that complete the program get a job offer. Unlock unlimited access to all content and features.
Go Pro