Given an integer array nums, return true if you can partition the array into two subsets such that the sum of the elements in both subsets is equal or false otherwise.
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:
1 <= nums.length <= 2001 <= nums[i] <= 100The key idea behind Partition Equal Subset Sum is to determine whether the array can be divided into two subsets with the same total sum. First, compute the total sum of the array. If the sum is odd, it is impossible to split it into two equal subsets. Otherwise, the problem reduces to checking whether a subset exists with sum target = totalSum / 2.
This becomes a classic subset sum dynamic programming problem. You can use a boolean DP approach where dp[i] represents whether a subset with sum i can be formed from the given numbers. Iteratively update the DP array for each element while traversing from right to left to avoid reuse of the same number.
An optimized 1D DP solution reduces space usage while maintaining efficiency. The overall time complexity is O(n * target), where n is the number of elements, and space complexity can be reduced to O(target).
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Dynamic Programming (Subset Sum) | O(n × target) | O(n × target) |
| Optimized 1D DP | O(n × target) | O(target) |
take U forward
This problem can be viewed as a variation of the subset sum problem. The idea is to determine if there is a subset of the given array whose sum is exactly half of the total sum. We use dynamic programming to store information about the previous computations, specifically a DP array where dp[i] indicates whether it's possible to achieve sum i using the elements of the array.
Time Complexity: O(N * target), where N is the number of elements in nums and target is half of the total sum.
Space Complexity: O(target), due to the DP array.
1def canPartition(nums):
2 total_sum = sum(nums)
3 if total_sum % 2 != 0:
4 return False
5 target = total_sum // 2
6 dp = [False] * (target + 1)
7 dp[0] = True
8 for num in nums:
9 for i in range(target, num - 1, -1):
10 dp[i] = dp[i] or dp[i - num]
11 return dp[target]The code starts by calculating the total sum of the array. If this sum is odd, it's impossible to split it into two equal subsets, so it returns false. Otherwise, it computes the target sum as half of the total sum and initializes a DP array dp of size target + 1. The DP array keeps track of which sums can be formed with the numbers encountered so far. It iterates through each number, updating the DP array to indicate whether a subset including that number can achieve each possible sum. Finally, it checks if the target sum is achievable.
This approach uses recursion along with memoization to solve the problem. We try to find a subset that totals half of the overall sum. As we attempt different combinations, we keep track of subproblem solutions we have already computed, which optimizes our solution by avoiding redundant computations.
Time Complexity: O(N * target), where N is the number of elements in nums and target is half of the total sum. The memoization ensures each unique call is computed only once.
Space Complexity: O(N * target) due to the recursive stack and memoization map.
1using System;
2using System.Collections.Generic;
3
4public class Solution {
public bool CanPartition(int[] nums) {
int total_sum = 0;
foreach (int num in nums) total_sum += num;
if (total_sum % 2 != 0) return false;
return CanPartition(nums, 0, total_sum / 2, new Dictionary<string, bool>());
}
private bool CanPartition(int[] nums, int index, int target, Dictionary<string, bool> memo) {
if (target == 0) return true;
if (index >= nums.Length) return false;
string memoKey = index + "," + target;
if (memo.ContainsKey(memoKey)) return memo[memoKey];
bool result = CanPartition(nums, index + 1, target, memo) || (target >= nums[index] && CanPartition(nums, index + 1, target - nums[index], memo));
memo[memoKey] = result;
return result;
}
}Watch expert explanations and walkthroughs
Practice problems asked by these companies to ace your technical interviews.
Explore More ProblemsJot down your thoughts, approach, and key learnings
Yes, Partition Equal Subset Sum is a popular dynamic programming interview problem and has been asked in coding interviews at companies like Amazon, Google, and Facebook. It tests understanding of subset sum and space-optimized DP techniques.
The optimal approach uses dynamic programming to reduce the problem to a subset sum check. By calculating whether a subset with sum equal to half of the total array sum exists, we can determine if the array can be partitioned into two equal subsets.
A boolean dynamic programming array is commonly used for this problem. The DP array tracks whether a specific subset sum can be formed as you iterate through the elements of the array.
Brute force or backtracking can attempt all possible subsets, but this approach is exponential and inefficient for large inputs. Dynamic programming significantly improves performance by storing intermediate subset results.
Similarly, this C# solution follows the logic of handling odd total sums by returning false immediately. A recursive function runs to attempt partitioning, with memoization used to store whether specific index and target combinations have already been resolved, enhancing efficiency.