Partition Equal Subset Sum medium
Problem Statement
Given a non-empty array nums
containing only positive integers, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal.
Example 1
Input: nums = [1,5,11,5] Output: true Explanation: The array can be partitioned as [1, 5, 5] and [11].
Example 2
Input: nums = [1,2,3,5] Output: false Explanation: The array cannot be partitioned into equal sum subsets.
Steps
-
Calculate the total sum: Find the sum of all elements in the input array
nums
. If the sum is odd, it's impossible to partition the array into two subsets with equal sums, so returnfalse
. -
Dynamic Programming: Use a boolean array
dp
of sizesum / 2 + 1
.dp[i]
will betrue
if there exists a subset ofnums
whose sum isi
, andfalse
otherwise. -
Initialization:
dp[0]
istrue
because an empty subset has a sum of 0. -
Iteration: Iterate through each number
num
innums
. For eachnum
, iterate throughdp
fromsum / 2
down tonum
. Ifdp[i - num]
istrue
(meaning there's a subset with sumi - num
), thendp[i]
can be set totrue
(because addingnum
to that subset results in a subset with sumi
). -
Result: After iterating through all numbers,
dp[sum / 2]
will indicate whether there exists a subset with a sum equal to half the total sum. Returndp[sum / 2]
.
Explanation
The dynamic programming approach cleverly uses a boolean array to track the possibility of achieving different subset sums. By iterating through the numbers and updating the dp
array, we efficiently determine whether a subset with half the total sum exists. The backward iteration (from sum/2
down to num
) is crucial to avoid double-counting subsets.
Code
#include <vector>
#include <numeric>
class Solution {
public:
bool canPartition(std::vector<int>& nums) {
int sum = std::accumulate(nums.begin(), nums.end(), 0);
if (sum % 2 != 0) return false; // Odd sum, impossible to partition
int target = sum / 2;
std::vector<bool> dp(target + 1, false);
dp[0] = true; // Empty subset has sum 0
for (int num : nums) {
for (int i = target; i >= num; --i) {
dp[i] = dp[i] || dp[i - num];
}
}
return dp[target];
}
};
Complexity
- Time Complexity: O(N * S), where N is the number of elements in
nums
and S is the sum of elements innums
. The nested loops dominate the runtime. - Space Complexity: O(S), where S is the sum of elements in
nums
. Thedp
array requires space proportional to the target sum.