Recursive program to print all subsets with given sum
Last Updated :
12 Jul, 2021
Given an array and a number, print all subsets with sum equal to given the sum.
Examples:
Input : arr[] = {2, 5, 8, 4, 6, 11}, sum = 13
Output :
5 8
2 11
2 5 6
Input : arr[] = {1, 5, 8, 4, 6, 11}, sum = 9
Output :
5 4
1 8
This problem is an extension of check if there is a subset with given sum. We recursively generate all subsets. We keep track of elements of current subset. If sum of elements in current subset becomes equal to given sum, we print the subset.
C++
// CPP program to print all subsets with given sum
#include <bits/stdc++.h>
using namespace std;
// The vector v stores current subset.
void printAllSubsetsRec(int arr[], int n, vector<int> v,
int sum)
{
// If remaining sum is 0, then print all
// elements of current subset.
if (sum == 0) {
for (auto x : v)
cout << x << " ";
cout << endl;
return;
}
// If no remaining elements,
if (n == 0)
return;
// We consider two cases for every element.
// a) We do not include last element.
// b) We include last element in current subset.
printAllSubsetsRec(arr, n - 1, v, sum);
v.push_back(arr[n - 1]);
printAllSubsetsRec(arr, n - 1, v, sum - arr[n - 1]);
}
// Wrapper over printAllSubsetsRec()
void printAllSubsets(int arr[], int n, int sum)
{
vector<int> v;
printAllSubsetsRec(arr, n, v, sum);
}
// Driver code
int main()
{
int arr[] = { 2, 5, 8, 4, 6, 11 };
int sum = 13;
int n = sizeof(arr) / sizeof(arr[0]);
printAllSubsets(arr, n, sum);
return 0;
}
Java
// Java program to print all subsets with given sum
import java.util.*;
class Solution
{
// The vector v stores current subset.
static void printAllSubsetsRec(int arr[], int n, Vector<Integer> v,
int sum)
{
// If remaining sum is 0, then print all
// elements of current subset.
if (sum == 0) {
for (int i=0;i<v.size();i++)
System.out.print( v.get(i) + " ");
System.out.println();
return;
}
// If no remaining elements,
if (n == 0)
return;
// We consider two cases for every element.
// a) We do not include last element.
// b) We include last element in current subset.
printAllSubsetsRec(arr, n - 1, v, sum);
Vector<Integer> v1=new Vector<Integer>(v);
v1.add(arr[n - 1]);
printAllSubsetsRec(arr, n - 1, v1, sum - arr[n - 1]);
}
// Wrapper over printAllSubsetsRec()
static void printAllSubsets(int arr[], int n, int sum)
{
Vector<Integer> v= new Vector<Integer>();
printAllSubsetsRec(arr, n, v, sum);
}
// Driver code
public static void main(String args[])
{
int arr[] = { 2, 5, 8, 4, 6, 11 };
int sum = 13;
int n = arr.length;
printAllSubsets(arr, n, sum);
}
}
//contributed by Arnab Kundu
Python3
# Python program to print all subsets with given sum
# The vector v stores current subset.
def printAllSubsetsRec(arr, n, v, sum) :
# If remaining sum is 0, then print all
# elements of current subset.
if (sum == 0) :
for value in v :
print(value, end=" ")
print()
return
# If no remaining elements,
if (n == 0):
return
# We consider two cases for every element.
# a) We do not include last element.
# b) We include last element in current subset.
printAllSubsetsRec(arr, n - 1, v, sum)
v1 = [] + v
v1.append(arr[n - 1])
printAllSubsetsRec(arr, n - 1, v1, sum - arr[n - 1])
# Wrapper over printAllSubsetsRec()
def printAllSubsets(arr, n, sum):
v = []
printAllSubsetsRec(arr, n, v, sum)
# Driver code
arr = [ 2, 5, 8, 4, 6, 11 ]
sum = 13
n = len(arr)
printAllSubsets(arr, n, sum)
# This code is contributed by ihritik
C#
// C# program to print all subsets with given sum
using System;
using System.Collections.Generic;
class GFG
{
// The vector v stores current subset.
static void printAllSubsetsRec(int []arr, int n,
List<int> v, int sum)
{
// If remaining sum is 0, then print all
// elements of current subset.
if (sum == 0)
{
for (int i = 0; i < v.Count; i++)
Console.Write( v[i]+ " ");
Console.WriteLine();
return;
}
// If no remaining elements,
if (n == 0)
return;
// We consider two cases for every element.
// a) We do not include last element.
// b) We include last element in current subset.
printAllSubsetsRec(arr, n - 1, v, sum);
List<int> v1 = new List<int>(v);
v1.Add(arr[n - 1]);
printAllSubsetsRec(arr, n - 1, v1, sum - arr[n - 1]);
}
// Wrapper over printAllSubsetsRec()
static void printAllSubsets(int []arr, int n, int sum)
{
List<int> v = new List<int>();
printAllSubsetsRec(arr, n, v, sum);
}
// Driver code
public static void Main()
{
int []arr = { 2, 5, 8, 4, 6, 11 };
int sum = 13;
int n = arr.Length;
printAllSubsets(arr, n, sum);
}
}
// This code is contributed by Rajput-Ji
PHP
<?php
// PHP program to print all subsets with given sum
// The vector v stores current subset.
function printAllSubsetsRec($arr, $n, $v, $sum)
{
// If remaining sum is 0, then print all
// elements of current subset.
if ($sum == 0)
{
for ($i = 0; $i < count($v); $i++)
echo $v[$i] . " ";
echo "\n";
return;
}
// If no remaining elements,
if ($n == 0)
return;
// We consider two cases for every element.
// a) We do not include last element.
// b) We include last element in current subset.
printAllSubsetsRec($arr, $n - 1, $v, $sum);
array_push($v, $arr[$n - 1]);
printAllSubsetsRec($arr, $n - 1, $v,
$sum - $arr[$n - 1]);
}
// Wrapper over printAllSubsetsRec()
function printAllSubsets($arr, $n, $sum)
{
$v = array();
printAllSubsetsRec($arr, $n, $v, $sum);
}
// Driver code
$arr = array( 2, 5, 8, 4, 6, 11 );
$sum = 13;
$n = count($arr);
printAllSubsets($arr, $n, $sum);
// This code is contributed by mits
?>
JavaScript
<script>
// JavaScript Program for the above approach
// The vector v stores current subset.
function printAllSubsetsRec(arr, n, v, sum) {
// If remaining sum is 0, then print all
// elements of current subset.
if (sum == 0) {
for (let x of v)
document.write(x + " ");
document.write("<br>")
return;
}
// If no remaining elements,
if (n == 0)
return;
// We consider two cases for every element.
// a) We do not include last element.
// b) We include last element in current subset.
printAllSubsetsRec(arr, n - 1, v, sum);
v.push(arr[n - 1]);
printAllSubsetsRec(arr, n - 1, v, sum - arr[n - 1]);
v.pop();
}
// Wrapper over printAllSubsetsRec()
function printAllSubsets(arr, n, sum) {
let v = [];
printAllSubsetsRec(arr, n, v, sum);
}
// Driver code
let arr = [2, 5, 8, 4, 6, 11];
let sum = 13;
let n = arr.length;
printAllSubsets(arr, n, sum);
// This code is contributed by Potta Lokesh
</script>
Time Complexity : O(2n)
Please refer below post for an optimized solution based on Dynamic Programming.
Print all subsets with given sum using Dynamic Programming
Similar Reads
Print all subsets with given sum Given an array arr[] of non-negative integers and an integer target. The task is to print all subsets of the array whose sum is equal to the given target. Note: If no subset has a sum equal to target, print -1.Examples:Input: arr[] = [5, 2, 3, 10, 6, 8], target = 10Output: [ [5, 2, 3], [2, 8], [10]
15+ 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
Maximum size subset with given sum This is an extended version of the subset sum problem. Here we need to find the size of the maximum size subset whose sum is equal to the given sum. Examples: Input : set[] = {2, 3, 5, 7, 10, 15}, sum = 10 Output : 3 The largest sized subset with sum 10 is {2, 3, 5} Input : set[] = {1, 2, 3, 4, 5} s
8 min read
Sum of all subsets of a given size (=K) Given an array arr[] consisting of N integers and a positive integer K, the task is to find the sum of all the subsets of size K. Examples: Input: arr[] = {1, 2, 4, 5}, K = 2Output: 36Explanation:The subsets of size K(= 2) are = {1, 2}, {1, 4}, {1, 5}, {2, 4}, {2, 5}, {4, 5}. Now, the sum of all sub
7 min read
Subset array sum by generating all the subsets Given an array of size N and a sum, the task is to check whether some array elements can be added to sum to N . Note: At least one element should be included to form the sum.(i.e. sum cant be zero) Examples: Input: array = -1, 2, 4, 121, N = 5 Output: YES The array elements 2, 4, -1 can be added to
5 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
Largest subset with sum of every pair as prime Given an array A[], find a subset of maximum size in which sum of every pair of elements is a prime number. Print its length and the subset. Consider many queries for different arrays and maximum value of an element as 100000. Examples : Input : A[] = {2, 1, 2} Output : 2 1 2 Explanation : Here, we
13 min read
Print all subsets of given size of a set Generate all possible subsets of size r of the given array with distinct elements. Examples: Input : arr[] = {1, 2, 3, 4} r = 2Output : 1 2 1 3 1 4 2 3 2 4 3 4Input : arr[] = {10, 20, 30, 40, 50} r = 3Output : 10 20 30 10 20 40 10 20 50 10 30 40 10 30 50 10 40 50 20 30 40 20 30 50 20 40 50 30 40 50
15+ min read
Sum of the sums of all possible subsets Given an array a of size N. The task is to find the sum of the sums of all possible subsets. Examples: Input: a[] = {3, 7} Output: 20 The subsets are: {3} {7} {3, 7} {3, 7} = 10 {3} = 3 {7} = 7 10 + 3 + 7 = 20 Input: a[] = {10, 16, 14, 9} Output: 392 Naive Approach: A naive approach is to find all t
7 min read
Count of subsets with sum equal to X | Set-2 Given an array arr[] of length N and an integer X, the task is to find the number of subsets with a sum equal to X. Examples: Input: arr[] = {1, 2, 3, 3}, X = 6 Output: 3 Explanation: All the possible subsets are {1, 2, 3}, {1, 2, 3} and {3, 3}. Input: arr[] = {1, 1, 1, 1}, X = 1 Output: 4 Recommend
13 min read