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.
Constraints:
nums
contain duplicate numbers?
nums
can contain duplicates.This problem can be approached using dynamic programming (DP). The idea is:
target
.dp
where dp[i]
will be True
if a subset with sum i
can be formed using the elements of the array.dp[0]
is True
because a sum of 0 can always be formed (with the empty subset).nums
and update the DP array backwards (to avoid using the same element more than once in the subset).def canPartition(nums):
total_sum = sum(nums)
# If the total sum is odd, it's not possible to partition into two equal subsets
if total_sum % 2 != 0:
return False
target = total_sum // 2
n = len(nums)
# Initialize a DP array of size target + 1
dp = [False] * (target + 1)
dp[0] = True # We can always form a zero sum with an empty subset
# Update the dp array for each number in nums
for num in nums:
for i in range(target, num - 1, -1):
dp[i] = dp[i] or dp[i - num]
# The answer will be whether we can form the target sum using elements of nums
return dp[target]
# Example usage:
print(canPartition([1, 5, 11, 5])) # Output: True
print(canPartition([1, 2, 3, 5])) # Output: False
The time complexity of this solution is (O(n \times target)), where n
is the number of elements in nums
and target
is half the sum of elements in nums
. Here:
dp
array takes (O(n \times target)).dp
array.Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?