Print equal sum sets of array (Partition problem) | Set 1
Last Updated :
14 Sep, 2022
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.
We have already discussed a solution in Partition Problem to find if array can be partitioned or not. In this post, we print two sets that are also printed. We post pass two vectors set1 and set2 and two sum variables sum1 and sum2. Traverse the array recursively.
At every array position there are two choices: either add the current element to set 1 or to set 2. Recursively call for both the conditions and update the vectors set1 and set2 accordingly. If the current element is added to set 1 then add the current element to sum1 and insert it in vector set 1. Repeat the same if the current element is included in set 2. At the end of array traversal compare both the sums. If both the sums are equal then print both the vectors otherwise backtrack to check other possibilities.
Implementation:
C++
// CPP program to print equal sum two subsets of
// an array if it can be partitioned into subsets.
#include <bits/stdc++.h>
using namespace std;
/// Function to print the equal sum sets of the array.
void printSets(vector<int> set1, vector<int> set2)
{
int i;
/// Print set 1.
for (i = 0; i < set1.size(); i++) {
cout << set1[i] << " ";
}
cout << "\n";
/// Print set 2.
for (i = 0; i < set2.size(); i++) {
cout << set2[i] << " ";
}
}
/// Utility function to find the sets of the array which
/// have equal sum.
bool findSets(int arr[], int n, vector<int>& set1,
vector<int>& set2, int sum1, int sum2,
int pos)
{
/// If entire array is traversed, compare both the sums.
if (pos == n) {
/// If sums are equal print both sets and return
/// true to show sets are found.
if (sum1 == sum2) {
printSets(set1, set2);
return true;
}
/// If sums are not equal then return sets are not
/// found.
else
return false;
}
/// Add current element to set 1.
set1.push_back(arr[pos]);
/// Recursive call after adding current element to
/// set 1.
bool res = findSets(arr, n, set1, set2, sum1 + arr[pos],
sum2, pos + 1);
/// If this inclusion results in equal sum sets
/// partition then return true to show desired sets are
/// found.
if (res)
return res;
/// If not then backtrack by removing current element
/// from set1 and include it in set 2.
set1.pop_back();
set2.push_back(arr[pos]);
/// Recursive call after including current element to
/// set 2.
res = findSets(arr, n, set1, set2, sum1,
sum2 + arr[pos], pos + 1);
if (res == false)
set2.pop_back();
return res;
}
/// Return true if array arr can be partitioned
/// into two equal sum sets or not.
bool isPartitionPoss(int arr[], int n)
{
/// Calculate sum of elements in array.
int sum = 0;
for (int i = 0; i < n; i++)
sum += arr[i];
/// If sum is odd then array cannot be
/// partitioned.
if (sum % 2 != 0)
return false;
/// Declare vectors to store both the sets.
vector<int> set1, set2;
/// Find both the sets.
return findSets(arr, n, set1, set2, 0, 0, 0);
}
// Driver code
int main()
{
int arr[] = { 5, 5, 1, 11 };
int n = sizeof(arr) / sizeof(arr[0]);
if (!isPartitionPoss(arr, n)) {
cout << "-1";
}
return 0;
}
Java
// Java program to print equal sum two subsets
// of an array if it can be partitioned into
// subsets.
import java.io.*;
import java.util.*;
public class GFG {
// Declare Lists to store both
// the sets.
static List<Integer> set1 = new ArrayList<Integer>();
static List<Integer> set2 = new ArrayList<Integer>();
/// Function to print the equal sum sets
// of the array.
static void printSets()
{
int i;
/// Print set 1.
for (i = 0; i < set1.size(); i++) {
System.out.print(set1.get(i) + " ");
}
System.out.println();
/// Print set 2.
for (i = 0; i < set2.size(); i++) {
System.out.print(set2.get(i) + " ");
}
}
// Utility function to find the sets
// of the array which have equal sum.
static boolean findSets(Integer[] arr, int n,
int sum1, int sum2,
int pos)
{
// If entire array is traversed,
// compare both the sums.
if (pos == n) {
// If sums are equal print
// both sets and return true
// to show sets are found.
if (sum1 == sum2) {
printSets();
return true;
}
// If sums are not equal
// then return sets are not
// found.
else
return false;
}
// Add current element to set 1.
set1.add(arr[pos]);
// Recursive call after adding
// current element to set 1.
boolean res = findSets(arr, n, sum1 + arr[pos],
sum2, pos + 1);
// If this inclusion results in
// equal sum sets partition then
// return true to show desired
// sets are found.
if (res == true)
return res;
// If not then backtrack by removing
// current element from set1 and
// include it in set 2.
set1.remove(set1.size() - 1);
set2.add(arr[pos]);
// Recursive call after including
// current element to set 2.
res = findSets(arr, n, sum1, sum2
+ arr[pos],
pos + 1);
if (res == false)
set2.remove(set2.size() - 1);
return res;
}
// Return true if array arr can be
// partitioned into two equal sum
// sets or not.
static boolean isPartitionPoss(Integer[] arr,
int n)
{
// Calculate sum of elements in
// array.
int sum = 0;
for (int i = 0; i < n; i++)
sum += arr[i];
// If sum is odd then array cannot
// be partitioned.
if (sum % 2 != 0)
return false;
/// Find both the sets.
return findSets(arr, n, 0, 0, 0);
}
// Driver code
public static void main(String args[])
{
Integer[] arr = { 5, 5, 1, 11 };
int n = arr.length;
if (isPartitionPoss(arr, n) == false) {
System.out.print("-1");
}
}
}
// This code is contributed by Manish Shaw
// (manishshaw1)
Python3
# Python3 program to print equal sum two subsets of
# an array if it can be partitioned into subsets.
# Function to print the equal sum sets of the array.
def printSets(set1, set2) :
# Print set 1.
for i in range(0, len(set1)) :
print ("{} ".format(set1[i]), end ="");
print ("")
# Print set 2.
for i in range(0, len(set2)) :
print ("{} ".format(set2[i]), end ="");
print ("")
# Utility function to find the sets of the
# array which have equal sum.
def findSets(arr, n, set1, set2, sum1, sum2, pos) :
# If entire array is traversed, compare both
# the sums.
if (pos == n) :
# If sums are equal print both sets and
# return true to show sets are found.
if (sum1 == sum2) :
printSets(set1, set2)
return True
# If sums are not equal then return
# sets are not found.
else :
return False
# Add current element to set 1.
set1.append(arr[pos])
# Recursive call after adding current
# element to set 1.
res = findSets(arr, n, set1, set2,
sum1 + arr[pos], sum2, pos + 1)
# If this inclusion results in an equal sum
# sets partition then return true to show
# desired sets are found.
if (res) :
return res
# If not then backtrack by removing current
# element from set1 and include it in set 2.
set1.pop()
set2.append(arr[pos])
# Recursive call after including current
# element to set 2 and removing the element
# from set 2 if it returns False
res= findSets(arr, n, set1, set2, sum1,
sum2 + arr[pos], pos + 1)
if not res:
set2.pop()
return res
# Return true if array arr can be partitioned
# into two equal sum sets or not.
def isPartitionPoss(arr, n) :
# Calculate sum of elements in array.
sum = 0
for i in range(0, n):
sum += arr[i]
# If sum is odd then array cannot be
# partitioned.
if (sum % 2 != 0) :
return False
# Declare vectors to store both the sets.
set1 = []
set2 = []
# Find both the sets.
return findSets(arr, n, set1, set2, 0, 0, 0)
# Driver code
arr = [5, 5, 1, 11]
n = len(arr)
if (isPartitionPoss(arr, n) == False) :
print ("-1")
# This code is contributed by Manish Shaw
# (manishshaw1)
C#
// C# program to print equal sum two subsets
// of an array if it can be partitioned into
// subsets.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections;
class GFG {
/// Function to print the equal sum sets
// of the array.
static void printSets(List<int> set1,
List<int> set2)
{
int i;
/// Print set 1.
for (i = 0; i < set1.Count; i++) {
Console.Write(set1[i] + " ");
}
Console.WriteLine();
/// Print set 2.
for (i = 0; i < set2.Count; i++) {
Console.Write(set2[i] + " ");
}
}
// Utility function to find the sets
// of the array which have equal sum.
static bool findSets(int[] arr, int n,
ref List<int> set1,
ref List<int> set2,
int sum1, int sum2,
int pos)
{
// If entire array is traversed,
// compare both the sums.
if (pos == n) {
// If sums are equal print
// both sets and return true
// to show sets are found.
if (sum1 == sum2) {
printSets(set1, set2);
return true;
}
// If sums are not equal
// then return sets are not
// found.
else
return false;
}
// Add current element to set 1.
set1.Add(arr[pos]);
// Recursive call after adding
// current element to set 1.
bool res = findSets(arr, n, ref set1,
ref set2, sum1 + arr[pos],
sum2, pos + 1);
// If this inclusion results in
// equal sum sets partition then
// return true to show desired
// sets are found.
if (res == true)
return res;
// If not then backtrack by removing
// current element from set1 and
// include it in set 2.
set1.RemoveAt(set1.Count - 1);
set2.Add(arr[pos]);
// Recursive call after including
// current element to set 2.
res = findSets(arr, n, ref set1,
ref set2, sum1, sum2
+ arr[pos],
pos + 1);
if (res == false)
set2.RemoveAt(set2.Count - 1);
return res;
}
// Return true if array arr can be
// partitioned into two equal sum
// sets or not.
static bool isPartitionPoss(int[] arr,
int n)
{
// Calculate sum of elements in
// array.
int sum = 0;
for (int i = 0; i < n; i++)
sum += arr[i];
// If sum is odd then array cannot
// be partitioned.
if (sum % 2 != 0)
return false;
// Declare Lists to store both
// the sets.
List<int> set1 = new List<int>();
List<int> set2 = new List<int>();
/// Find both the sets.
return findSets(arr, n, ref set1,
ref set2, 0, 0, 0);
}
// Driver code
public static void Main()
{
int[] arr = { 5, 5, 1, 11 };
int n = arr.Length;
if (isPartitionPoss(arr, n) == false) {
Console.Write("-1");
}
}
}
// This code is contributed by Manish Shaw
// (manishshaw1)
PHP
<?php
// PHP program to print equal sum
// two subsets of an array if it
// can be partitioned into subsets.
// Function to print the equal
// sum sets of the array.
function printSets($set1, $set2)
{
$i = 0;
// Print set 1.
for ($i = 0; $i < count($set1); $i++)
{
echo ($set1[$i]." ");
}
echo ("\n");
// Print set 2.
for ($i = 0; $i < count($set2); $i++)
{
echo ($set2[$i]." ");
}
}
// Utility function to find the
// sets of the array which have
// equal sum.
function findSets($arr, $n, &$set1,
&$set2, $sum1,
$sum2, $pos)
{
// If entire array is traversed,
// compare both the sums.
if ($pos == $n)
{
// If sums are equal print
// both sets and return
// true to show sets are found.
if ($sum1 == $sum2)
{
printSets($set1, $set2);
return true;
}
// If sums are not equal then
// return sets are not found.
else
return false;
}
// Add current element to set 1.
array_push($set1, $arr[$pos]);
// Recursive call after adding
// current element to set 1.
$res = findSets($arr, $n, $set1, $set2,
$sum1 + $arr[$pos],
$sum2, $pos + 1);
// If this inclusion results in
// equal sum sets partition then
// return true to show desired
// sets are found.
if ($res)
return $res;
// If not then backtrack by
// removing current element
// from set1 and include it
// in set 2.
array_pop($set1);
array_push($set2, $arr[$pos]);
// Recursive call after including
// current element to set 2.
$res = findSets($arr, $n, $set1, $set2,
$sum1, $sum2 + $arr[$pos],
$pos + 1);
if ($res == false){
array_pop($set2);
}
return $res;
}
// Return true if array arr
// can be partitioned into
// two equal sum sets or not.
function isPartitionPoss($arr, $n)
{
// Calculate sum of
// elements in array.
$sum = 0;
for ($i = 0; $i < $n; $i++)
$sum += $arr[$i];
// If sum is odd then array
// cannot be partitioned.
if ($sum % 2 != 0)
return false;
// Declare vectors to
// store both the sets.
$set1 = array();
$set2 = array();
// Find both the sets.
return findSets($arr, $n, $set1,
$set2, 0, 0, 0);
}
// Driver code
$arr= array( 5, 5, 1, 11 );
$n = count($arr);
if (isPartitionPoss($arr, $n) == false)
echo ("-1");
// This code is contributed by
// Manish Shaw (manishshaw1)
?>
JavaScript
<script>
// Javascript program to print equal sum two
// subsets of an array if it can be partitioned
// into subsets.
// Function to print the equal sum
// sets of the array.
function printSets(set1, set2)
{
var i;
// Print set 1.
for(i = 0; i < set1.length; i++)
{
document.write( set1[i] + " ");
}
document.write("<br>");
// Print set 2.
for(i = 0; i < set2.length; i++)
{
document.write( set2[i] + " ");
}
}
// Utility function to find the sets of the
// array which have equal sum.
function findSets(arr, n, set1, set2,
sum1, sum2, pos)
{
// If entire array is traversed,
// compare both the sums.
if (pos == n)
{
// If sums are equal print both sets
// and return true to show sets are found.
if (sum1 == sum2)
{
printSets(set1, set2);
return true;
}
// If sums are not equal then
// return sets are not found.
else
return false;
}
// Add current element to set 1.
set1.push(arr[pos]);
// Recursive call after adding current
// element to set 1.
var res = findSets(arr, n, set1, set2,
sum1 + arr[pos],
sum2, pos + 1);
// If this inclusion results in equal sum
// sets partition then return true to show
// desired sets are found.
if (res)
return res;
// If not then backtrack by removing
// current element from set1 and
// include it in set 2.
set1.pop();
set2.push(arr[pos]);
// Recursive call after including
// current element to set 2.
res = findSets(arr, n, set1, set2,
sum1, sum2 + arr[pos],
pos + 1);
if (res == false)
set2.pop();
return res;
}
// Return true if array arr can be partitioned
// into two equal sum sets or not.
function isPartitionPoss(arr, n)
{
// Calculate sum of elements in array.
var sum = 0;
for(var i = 0; i < n; i++)
sum += arr[i];
// If sum is odd then array cannot be
// partitioned.
if (sum % 2 != 0)
return false;
// Declare vectors to store both the sets.
var set1 = [];
var set2 = [];
// Find both the sets.
return findSets(arr, n, set1, set2, 0, 0, 0);
}
// Driver code
var arr = [ 5, 5, 1, 11 ];
var n = arr.length;
if (!isPartitionPoss(arr, n))
{
document.write( "-1");
}
// This code is contributed by SoumikMondal
</script>
Complexity Analysis:
- Time Complexity: Exponential O(2^n)
- Auxiliary Space: O(n) (Without considering size of function call stack)
Print equal sum sets of array (Partition Problem) | Set 2
Similar Reads
Print equal sum sets of Array (Partition Problem) using Dynamic Programming Given an array arr[]. Determine whether it is possible to split the array into two sets such that the sum of elements in both sets is equal. If it is possible, then print both 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}Su
13 min read
Partition a Set into Two Subsets of Equal Sum 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: Th
15+ min read
Partition of a set into K subsets with equal sum Given an integer array arr[] and an integer k, the task is to check if it is possible to divide the given array into k non-empty subsets of equal sum such that every array element is part of a single subset.Examples: Input: arr[] = [2, 1, 4, 5, 6], k = 3 Output: trueExplanation: Possible subsets of
9 min read
Minimum Subset sum difference problem with Subset partitioning Given a set of N integers with up to 40 elements, the task is to partition the set into two subsets of equal size (or the closest possible), such that the difference between the sums of the subsets is minimized. If the size of the set is odd, one subset will have one more element than the other. If
13 min read
Print sums of all subsets of a given set Given an array of integers, print sums of all subsets in it. Output sums can be printed in any order.Examples : Input: arr[] = {2, 3}Output: 0 2 3 5Explanation: All subsets of this array are - {{}, {2}, {3}, {2, 3}}, having sums - 0, 2, 3 and 5 respectively.Input: arr[] = {2, 4, 5}Output: 0 2 4 5 6
10 min read
Print all subsets of a given Set or Array Given an array arr of size n, your task is to print all the subsets of the array in lexicographical order.A subset is any selection of elements from an array, where the order does not matter, and no element appears more than once. It can include any number of elements, from none (the empty subset) t
12 min read
Subset sum problem where Array sum is at most N Given an array arr[] of size N such that the sum of all the array elements does not exceed N, and array queries[] containing Q queries. For each query, the task is to find if there is a subset of the array whose sum is the same as queries[i]. Examples: Input: arr[] = {1, 0, 0, 0, 0, 2, 3}, queries[]
10 min read
Find maximum subset sum formed by partitioning any subset of array into 2 partitions with equal sum Given an array A containing N elements. Partition any subset of this array into two disjoint subsets such that both the subsets have an identical sum. Obtain the maximum sum that can be obtained after partitioning. Note: It is not necessary to partition the entire array, that is any element might no
15+ min read
Split numbers from 1 to N into two equal sum subsets Given an integer N, the task is to divide the numbers from 1 to N into two nonempty subsets such that the sum of elements in the set is equal. Print the element in the subset. If we can't form any subset then print -1. Examples: Input N = 4 Output: Size of subset 1 is: 2 Elements of the subset are:
10 min read
Partition of a set into k subsets with equal sum using BitMask and DP Given an integer array arr[] and an integer k, the task is to check if it is possible to divide the given array into k non-empty subsets of equal sum such that every array element is part of a single subset.Examples: Input: arr[] = [2, 1, 4, 5, 6], k = 3 Output: trueExplanation: Possible subsets of
9 min read