18. Partition Equal Subset Sum
The problem can be found at the following link: Question Link
Problem Description
Given an array arr[]
, determine if it can be partitioned into two subsets such that the sum of elements in both parts is the same.
Each element must be in exactly one subset.
Examples
Example 1:
Input:
arr = [1, 5, 11, 5]
Output:
True
Explanation:
The array can be partitioned into [1, 5, 5] and [11], both having the sum 11.
Example 2:
Input:
arr = [1, 3, 5]
Output:
False
Explanation:
There is no way to split the array into two subsets with equal sum.
Constraints:
$(1 \leq arr.size \leq 100)$
$(1 \leq arr[i] \leq 200)$
Optimized 1D Dynamic Programming
Approach:
Calculate the total sum of the array. If it's odd, return false (cannot be evenly split).
Use a DP table (
dp[j]
), wheredp[j]
tells if we can form sumj
.Initialize
dp[0] = true
, since a sum of 0 is always possible.Iterate through each element in the array:
Update
dp[j] = dp[j] OR dp[j - num]
forj = target
down tonum
.
If
dp[target]
is true, we can partition the array into two subsets with equal sum.
Time and Auxiliary Space Complexity
Expected Time Complexity: $O(N \times sum)$, as we iterate over the elements and process sums up to
sum/2
.Expected Auxiliary Space Complexity: $O(sum)$, as we use a 1D DP array of size
sum/2 + 1
.
Code (C++)
class Solution {
public:
bool equalPartition(vector<int>& arr) {
int sum = accumulate(arr.begin(), arr.end(), 0);
if (sum % 2) return false;
int target = sum / 2;
vector<bool> dp(target + 1, false);
dp[0] = true;
for (int num : arr)
for (int j = target; j >= num; --j)
dp[j] = dp[j] || dp[j - num];
return dp[target];
}
};
Code (Java)
class Solution {
static boolean equalPartition(int[] arr) {
int sum = Arrays.stream(arr).sum();
if (sum % 2 != 0) return false;
int target = sum / 2;
boolean[] dp = new boolean[target + 1];
dp[0] = true;
for (int num : arr)
for (int j = target; j >= num; --j)
dp[j] |= dp[j - num];
return dp[target];
}
}
Code (Python)
class Solution:
def equalPartition(self, arr):
s = sum(arr)
if s % 2: return False
target, dp = s // 2, [False] * (s // 2 + 1)
dp[0] = True
for num in arr:
for j in range(target, num - 1, -1):
dp[j] |= dp[j - num]
return dp[target]
Contribution and Support:
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Letβs make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated