Open In App

Partition a Set into Two Subsets of Equal Sum

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
144 Likes
Like
Report

Given an array arr[], the task is to check if it can be partitioned into two parts such that the sum of elements in both parts is the same.
Note: Each element is present in either the first subset or the second subset, but not in both.

Examples: 

Input: arr[] = [1, 5, 11, 5]
Output: true 
Explanation: The array can be partitioned as [1, 5, 5] and [11]

Input: arr[] = [1, 5, 3]
Output: false
Explanation: The array cannot be partitioned into equal sum sets.

The following are the two main steps to solve this problem:

  • Calculate the sum of the array. If the sum is odd, this cannot be two subsets with an equal sum, so return false. 
  • If the sum of the array elements is even, calculate sum/2 and find a subset of the array with a sum equal to sum/2
    The first step is simple. The second step is crucial, it can be solved either using recursion or Dynamic Programming.

[Naive Approach] Using Recursion - O(2^n) Time and O(n) Space

Let isSubsetSum(arr, n, sum/2) be the function that returns true if there is a subset of arr[0..n-1] with sum equal to sum/2
The isSubsetSum problem can be divided into two subproblems

  •  isSubsetSum() without considering last element (reducing n to n-1)
  •  isSubsetSum() considering the last element (reducing sum/2 by arr[n-1] and n to n-1)

If any of the above subproblems return true, then return true. 
isSubsetSum (arr, n, sum/2) = isSubsetSum (arr, n-1, sum/2) OR isSubsetSum (arr, n-1, sum/2 - arr[n-1])

Step by Step implementation:

  1. First, check if the sum of the elements is even or not.
  2. After checking, call the recursive function isSubsetSum with parameters as input array, array size, and sum/2
    • If the sum is equal to zero then return true (Base case)
    • If n is equal to 0 and sum is not equal to zero then return false (Base case)
    • Check if the value of the last element is greater than the remaining sum then call this function again by removing the last element
    • Else call this function again for both the cases stated above and return true, if anyone of them returns true
  3. Print the answer
C++
Java Python C# JavaScript

Output
True

[Better Approach 1] Using Top-Down DP (Memoization) - O(sum*n) Time and O(sum*n) Space

If we notice carefully, we can observe that the above recursive solution holds the following two properties of Dynamic Programming.

1. Optimal Substructure:

The solution to the subset sum problem can be derived from the optimal solutions of smaller subproblems. Specifically, for any given n (the number of elements considered) and a target sum, we can express the recursive relation as follows:

  • If the last element (arr[n-1]) is greater than sum, we cannot include it in our subset isSubsetSum(arr,n,sum) = isSubsetSum(arr,n-1,sum)

If the last element is less than or equal to sum, we have two choices:

  • Include the last element in the subset, isSubsetSum(arr, n, sum) = isSubsetSum(arr, n-1, sum-arr[n-1])
  • Exclude the last element, isSubsetSum(arr, n, sum) = isSubsetSum(arr, n-1, sum)

2. Overlapping Subproblems:

When implementing a recursive approach to solve the subset sum problem, we observe that many subproblems are computed multiple times.

  • The recursive solution involves changing two parameters: the current index in the array (n) and the current target sum (sum). We need to track both parameters, so we create a 2D array of size (n+1) x (sum + 1) because the value of n will be in the range [0, n] and sum will be in the range [0, sum].
  • We initialize the 2D array with -1 to indicate that no subproblems have been computed yet.
  • We check if the value at memo[n][sum] is -1. If it is, we proceed to compute the result. otherwise, we return the stored result.
C++
Java Python C# JavaScript

Output
True

[Better Approach 2] Using Bottom-Up DP (Tabulation) - O(sum*n) Time and O(sum*n) Space

The approach is similar to the previous one. just instead of breaking down the problem recursively, we iteratively build up the solution by calculating in bottom-up manner.

So we will create a 2D array of size (n + 1) * (sum + 1) of type boolean. The state dp[i][j] will be true if there exists a subset of elements from arr[0 . . . i] with sum = ‘j’. 

The dynamic programming relation is as follows: 

if (arr[i-1]>j)
    dp[i][j]=dp[i-1][j]
else 
dp[i][j]=dp[i-1][j] OR dp[i-1][j-arr[i-1]]

This means that if the current element has a value greater than the ‘current sum value’ we will copy the answer for previous cases and if the current sum value is greater than the ‘ith’ element we will see if any of the previous states have already computed the sum = j OR any previous states computed a value ‘j – arr[i]’ which will solve our purpose.

C++
Java Python C# JavaScript

Output
True

[Expected Approach] Using Space Optimized DP - O(sum*n) Time and O(sum) Space

In previous approach of dynamic programming we have derive the relation between states as given below:

if (arr[i-1]>j)
    dp[i][j] = dp[i-1][j]
else 
    dp[i][j] = dp[i-1][j] OR dp[i-1][j-arr[i-1]]

If we observe that for calculating current dp[i][j] state we only need previous row dp[i-1][j] or dp[i-1][j-arr[i-1]]. There is no need to store all the previous states just one previous state is used to compute result.

Approach:

  • Define two arrays prev and curr of size sum+1 to store the just previous row result and current row result respectively.
  • Once curr array is calculated then curr becomes our prev for the next row.
  • When all rows are processed the answer is stored in prev array.
C++
Java Python C# JavaScript

Output
True

Related Articles:


Partition a Set into Two Subsets of Equal Sum.

Similar Reads