Partition Equal Subset Sum
Medium
Given an integer array nums, determine whether you can partition the array into two subsets such that the sum of the elements in both subsets is equal.
Return true if such a partition is possible, otherwise return false.
Example 1
Input
nums = [1, 5, 11, 5]
Output
true
Explanation
The array can be partitioned into:
[1, 5, 5] = 11
[11] = 11
Both subsets have the same sum.
Example 2
Input
nums = [1, 2, 3, 5]
Output
false
Explanation
The total sum is 11, which is odd.
Therefore, it is impossible to divide the array into two subsets having equal sums.
Constraints
1 <= n <= 200
1 <= nums[i] <= 100
The array contains positive integers.
Hints:
Hint 1
First calculate the total sum of all elements.
If the total sum is odd, an equal partition is impossible.
Hint 2
If the total sum is even, the problem becomes:
Can we find a subset whose sum is totalSum / 2?
Auto
Loading editor...
Input
Expected Output