Partition Equal Subset Sum

Updated on 20 June, 2025
Partition Equal Subset Sum header image

Problem Statement

In this problem, you are given an integer array nums. The task is to determine if it's possible to divide this array into two subsets such that the sum of numbers in each subset is equal. If such a partition exists, then return true, otherwise return false. The challenge lies in figuring out a way to split the array to meet the condition, given that the number values and the number of elements can vary widely.

Examples

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 <= 200
  • 1 <= nums[i] <= 100

Approach and Intuition

The problem essentially boils down to a known computational problem called the "Partition Problem" which is a special case of the "Subset Sum Problem". It's useful to view this as finding if there exists a subset in the array whose sum equals half of the total sum of the array.

  • Calculate the total sum of the array:
    • If the sum is odd, immediately return false because two equal integer sums can't form an odd number.
  • Target a subset with the sum equal to half of the total sum:
    • If such a subset exists, it implies the second subset also sums to this target (since the total is twice the target).
  • Use a dynamic programming approach to solve the subset-sum problem:
    • Create a boolean DP array where dp[i] will be true if a subset with sum i can be formed using elements of the array.
    • Update the DP array for each number in nums considering each potential subset sum from target down to the number itself.
  • Check dp[target]:
    • If dp[target] is true, this denotes that a subset with the required target sum exists.

With the constraints specified:

  • 1 <= nums.length <= 200
  • 1 <= nums[i] <= 100

Considering an upper bound of nums.length = 200 and nums[i] = 100, the maximum sum would be 200*100 = 20000. The DP approach, which involves creating a subset sum array up to this sum, becomes computationally feasible within these limits. Hence, the problem promises a tractable solution space for the given input size.

Solutions

  • C++
  • Java
  • Python
cpp
class Solution {
public:
    bool canEqualPartition(vector<int> &elements) {
        int sumAllElements = 0;
        for (int element : elements) {
            sumAllElements += element;
        }
        if (sumAllElements % 2 != 0) return false;
        int targetSum = sumAllElements / 2;
        int elemCount = elements.size();
        vector<bool> dpState(targetSum + 1, false);
        dpState[0] = true;
        for (int value : elements) {
            for (int i = targetSum; i >= value; i--) {
                dpState[i] = dpState[i] || dpState[i - value];
            }
        }
        return dpState[targetSum];
    }
};

This summary explains how to determine if a given set of integers can be partitioned into two subsets with equal sum using a dynamic programming approach in C++.

The function canEqualPartition in the Solution class checks whether a partition with equal subset sums is possible for a vector of integers named elements.

  • First, calculate the total sum of all elements in the array. If this total sum is not even, it's not possible to divide it into two equal parts, so the function returns false.
  • Calculate the target sum which is half of the total sum.
  • Initialize a boolean vector dpState with a size of targetSum + 1 to keep track of possible sums up to the target. Set the first element of this vector to true to represent that a sum of 0 is always possible.
  • Iterate over each element in the elements vector. For each element, iterate backwards from targetSum to the value of the current element. Update the dpState by checking if each respective sum minus the current element value was previously achievable.
  • At the end of the iterations, the value at dpState[targetSum] indicates whether it is possible to achieve a sum equivalent to the target sum using the elements of the array.

If dpState[targetSum] is true, the function returns true, indicating that the array can be split into two subsets with equal sum. Otherwise, it returns false. This approach efficiently determines the possibility of partitioning using dynamic programming to progressively build up a solution to larger subproblems using the solutions to smaller subproblems.

java
class Solution {
    public boolean canDivideIntoEqualSums(int[] array) {
        if (array.length == 0)
            return false;
        int sum = 0;
        // Calculate total sum of array elements
        for (int value : array) {
            sum += value;
        }
        // Check if it's possible to divide the array into two parts with equal sum
        if (sum % 2 != 0) return false;
        int neededSum = sum / 2;
        boolean[] sumPossible = new boolean[neededSum + 1];
        sumPossible[0] = true;
        for (int element : array) {
            for (int j = neededSum; j >= element; j--) {
                sumPossible[j] |= sumPossible[j - element];
            }
        }
        return sumPossible[neededSum];
    }
}

The solution provided in Java addresses the problem of determining if a given array can be partitioned into two subsets such that the sums of the numbers in both subsets are equal.

Here's how the solution is structured and works:

  1. Initially check if the array is empty. If it is, return false as a partition isn't possible.
  2. Compute the total sum of all elements in the array.
  3. If the total sum is an odd number, immediately return false, as it's impossible to split an odd number equally.
  4. Calculate the target sum each subset must reach, which is half of the total sum.
  5. Create a boolean array, sumPossible, to store possible subset sums up to the target sum. Initialize the first element (sumPossible[0]) as true, indicating that a sum of 0 is always attainable with an empty subset.
  6. Use dynamic programming to fill out the sumPossible array. For each element in the given array, update the sumPossible array in reverse order from the target sum down to the value of the array element.
  7. For each value and index, update sumPossible[j] to true if sumPossible[j - element] was true, indicating that adding the current element to a subset that was previously possible gives a new possible subset.
  8. After processing all elements, check sumPossible at index corresponding to the target sum (neededSum). If true, it means there exists a subset of the provided array which sums up to the required target, making it possible to partition the array into two equal sum subsets.

This approach ensures an efficient check using dynamic programming, avoiding the need for generating all possible subsets explicitly, which would be computationally prohibitive for larger arrays.

python
class Solution:
    def canDivide(self, numbers: List[int]) -> bool:
        total = sum(numbers)
        
        if total % 2 != 0:
            return False
        target = total // 2
        
        dp_table = [False] * (target + 1)
        dp_table[0] = True
        
        for number in numbers:
            for i in range(target, number - 1, -1):
                dp_table[i] = dp_table[i] or dp_table[i - number]
                
        return dp_table[target]

The problem "Partition Equal Subset Sum" aims to determine whether a given set of numbers can be partitioned into two subsets such that the sums of the numbers in both subsets are equal. The provided Python solution utilizes dynamic programming to address this problem effectively.

In the solution:

  • Calculate the sum of all elements in the list. If this sum is not even, return False because two equal subsets cannot have a decimal sum.
  • Divide the total sum by 2 to determine the target sum for each subset.
  • Create a dynamic programming table named dp_table initialized with False values indicating whether a specific sum is achievable. The size of dp_table is target + 1, with the first element set to True because a sum of zero is always achievable (by choosing no elements).
  • Iterate through each number in the list. For each number, check from the target down to the value of the number (to prevent using a number more than once). Update dp_table[i] to True if either it was already True or if the subset sum i-number was achievable.
  • After processing all numbers, check the value of dp_table[target]. If True, the numbers can be partitioned into two equal sums. Otherwise, it's not possible.

By following this approach, the function canDivide determines the possibility of partitioning the input list into two subsets with equal sums, solving the problem efficiently with a time complexity that is linearly dependent on the number of items and the sum of the items in the list.

Comments

No comments yet.