Given an array arr[]. Determine whether it is possible to split the array into two sets such that the sum of elements in both the sets is equal. If it is possible, then print both the sets. If it is not possible then output -1.

Examples:

Input : arr = {5, 5, 1, 11} Output : Set 1 = {5, 5, 1}, Set 2 = {11} Sum of both the sets is 11 and equal. Input : arr = {1, 5, 3} Output : -1 No partitioning results in equal sum sets.

**Prerequisite: **Partition Problem

**Approach: ** In the previous post, a solution using recursion is discussed. In this post, a solution using Dynamic Programming is explained.

The idea is to declare two sets set 1 and set 2. To recover the solution, traverse the boolean dp table backwards starting from the final result dp[n][k], where n = number of elements and k = sum/2. Set 1 will consists of elements that contribute to sum k and other elements that do not contribute are added to set 2. Follow these steps at each position to recover the solution.

- Check if dp[i-1][sum] is true or not. If it is true, then current element does not contribute to sum k. Add this element to set 2. Update index i by i-1 and sum remains unchanged.
- If dp[i-1][sum] is false, then current element contribute to sum k. Add current element to set 1. Update index i by i-1 and sum by sum-arr[i-1].

Repeat above steps until each index position is traversed.

**Implementation: **

[sourcecode language=”CPP”]

// CPP program to print equal sum sets of array.

#include <bits/stdc++.h>

using namespace std;

// Function to print equal sum

// sets of array.

void printEqualSumSets(int arr[], int n)

{

int i, currSum;

// Finding sum of array elements

int sum = accumulate(arr, arr+n, 0);

// Check sum is even or odd. If odd

// then array cannot be partitioned.

// Print -1 and return.

if (sum & 1) {

cout << "-1";

return;

}

// Divide sum by 2 to find

// sum of two possible subsets.

int k = sum >> 1;

// Boolean DP table to store result

// of states.

// dp[i][j] = true if there is a

// subset of elements in first i elements

// of array that has sum equal to j.

bool dp[n + 1][k + 1];

// If number of elements are zero, then

// no sum can be obtained.

for (i = 1; i <= k; i++)

dp[0][i] = false;

// Sum 0 can be obtained by not selecting

// any element.

for (i = 0; i <= n; i++)

dp[i][0] = true;

// Fill the DP table in bottom up manner.

for (i = 1; i <= n; i++) {

for (currSum = 1; currSum <= k; currSum++) {

// Excluding current element.

dp[i][currSum] = dp[i – 1][currSum];

// Including current element

if (arr[i – 1] <= currSum)

dp[i][currSum] = dp[i][currSum] |

dp[i – 1][currSum – arr[i – 1]];

}

}

// Required sets set1 and set2.

vector<int> set1, set2;

// If partition is not possible print

// -1 and return.

if (!dp[n][k]) {

cout << "-1\n";

return;

}

// Start from last element in dp table.

i = n;

currSum = k;

while (i > 0 && currSum >= 0) {

// If current element does not

// contribute to k, then it belongs

// to set 2.

if (dp[i – 1][currSum]) {

i–;

set2.push_back(arr[i]);

}

// If current element contribute

// to k then it belongs to set 1.

else if (dp[i – 1][currSum – arr[i – 1]]) {

i–;

currSum -= arr[i];

set1.push_back(arr[i]);

}

}

// Print elements of both the sets.

cout << "Set 1 elements: ";

for (i = 0; i < set1.size(); i++)

cout << set1[i] << " ";

cout << "\nSet 2 elements: ";

for (i = 0; i < set2.size(); i++)

cout << set2[i] << " ";

}

// Driver program.

int main()

{

int arr[] = { 5, 5, 1, 11 };

int n = sizeof(arr) / sizeof(arr[0]);

printEqualSumSets(arr, n);

return 0;

}

[/sourcecode]

**Output:**

Set 1 elements: 1 5 5 Set 2 elements: 11

**Time Complexity: ** O(n*k), where k = sum(arr) / 2

**Auxiliary Space: ** O(n*k)

## Recommended Posts:

- Print equal sum sets of array (Partition problem) | Set 1
- Equal sum array partition excluding a given element
- Partition problem | DP-18
- The painter's partition problem
- The painter's partition problem | Set 2
- Check if it possible to partition in k subarrays with equal sum
- Print all pairs in an unsorted array with equal sum
- Optimal partition of an array into four parts
- Find a partition point in array
- Maximum average sum partition of an array
- Partition an array such into maximum increasing segments
- Minimum toggles to partition a binary array so that it has first 0s then 1s
- Perfect Sum Problem (Print all subsets with given sum)
- Find an element in array such that sum of left array is equal to sum of right array
- Print triplets with sum less than or equal to k

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.