# Perfect Sum Problem (Print all subsets with given sum)

• Difficulty Level : Hard
• Last Updated : 26 Apr, 2022

Given an array of integers and a sum, the task is to print all subsets of the given array with a sum equal to a given sum.
Examples:

```Input : arr[] = {2, 3, 5, 6, 8, 10}
sum = 10
Output : 5 2 3
2 8
10

Input : arr[] = {1, 2, 3, 4, 5}
sum = 10
Output : 4 3 2 1
5 3 2
5 4 1 ```

This problem is mainly an extension of Subset Sum Problem. Here we not only need to find if there is a subset with the given sum but also need to print all subsets with a given sum.
Like the previous post, we build a 2D array dp[][] such that dp[i][j] stores true if sum j is possible with array elements from 0 to i.
After filling dp[][], we recursively traverse it from dp[n-1][sum]. For the cell being traversed, we store the path before reaching it and consider two possibilities for the element.
1) Element is included in the current path.
2) Element is not included in the current path.
Whenever the sum becomes 0, we stop the recursive calls and print the current path.
Below is an implementation of the above idea.

## C++

 `// C++ program to count all subsets with` `// given sum.` `#include ` `using` `namespace` `std;`   `// dp[i][j] is going to store true if sum j is` `// possible with array elements from 0 to i.` `bool``** dp;`   `void` `display(``const` `vector<``int``>& v)` `{` `    ``for` `(``int` `i = 0; i < v.size(); ++i)` `        ``printf``(``"%d "``, v[i]);` `    ``printf``(``"\n"``);` `}`   `// A recursive function to print all subsets with the` `// help of dp[][]. Vector p[] stores current subset.` `void` `printSubsetsRec(``int` `arr[], ``int` `i, ``int` `sum, vector<``int``>& p)` `{` `    ``// If we reached end and sum is non-zero. We print` `    ``// p[] only if arr is equal to sum OR dp[sum]` `    ``// is true.` `    ``if` `(i == 0 && sum != 0 && dp[sum])` `    ``{` `        ``p.push_back(arr[i]);` `        ``// Display Only when Sum of elements of p is equal to sum` `          ``if` `(arr[i] == sum)` `              ``display(p);` `        ``return``;` `    ``}`   `    ``// If sum becomes 0` `    ``if` `(i == 0 && sum == 0)` `    ``{` `        ``display(p);` `        ``return``;` `    ``}`   `    ``// If given sum can be achieved after ignoring` `    ``// current element.` `    ``if` `(dp[i-1][sum])` `    ``{` `        ``// Create a new vector to store path` `        ``vector<``int``> b = p;` `        ``printSubsetsRec(arr, i-1, sum, b);` `    ``}`   `    ``// If given sum can be achieved after considering` `    ``// current element.` `    ``if` `(sum >= arr[i] && dp[i-1][sum-arr[i]])` `    ``{` `        ``p.push_back(arr[i]);` `        ``printSubsetsRec(arr, i-1, sum-arr[i], p);` `    ``}` `}`   `// Prints all subsets of arr[0..n-1] with sum 0.` `void` `printAllSubsets(``int` `arr[], ``int` `n, ``int` `sum)` `{` `    ``if` `(n == 0 || sum < 0)` `       ``return``;`   `    ``// Sum 0 can always be achieved with 0 elements` `    ``dp = ``new` `bool``*[n];` `    ``for` `(``int` `i=0; i p;` `    ``printSubsetsRec(arr, n-1, sum, p);` `}`   `// Driver code` `int` `main()` `{` `    ``int` `arr[] = {1, 2, 3, 4, 5};` `    ``int` `n = ``sizeof``(arr)/``sizeof``(arr);` `    ``int` `sum = 10;` `    ``printAllSubsets(arr, n, sum);` `    ``return` `0;` `}`

## Java

 `// A Java program to count all subsets with given sum.` `import` `java.util.ArrayList;` `public` `class` `SubSet_sum_problem` `{` `    ``// dp[i][j] is going to store true if sum j is` `    ``// possible with array elements from 0 to i.` `    ``static` `boolean``[][] dp;` `     `  `    ``static` `void` `display(ArrayList v)` `    ``{` `       ``System.out.println(v);` `    ``}` `     `  `    ``// A recursive function to print all subsets with the` `    ``// help of dp[][]. Vector p[] stores current subset.` `    ``static` `void` `printSubsetsRec(``int` `arr[], ``int` `i, ``int` `sum, ` `                                         ``ArrayList p)` `    ``{` `        ``// If we reached end and sum is non-zero. We print` `        ``// p[] only if arr is equal to sum OR dp[sum]` `        ``// is true.` `        ``if` `(i == ``0` `&& sum != ``0` `&& dp[``0``][sum])` `        ``{` `            ``p.add(arr[i]);` `            ``display(p);` `            ``p.clear();` `            ``return``;` `        ``}` `     `  `        ``// If sum becomes 0` `        ``if` `(i == ``0` `&& sum == ``0``)` `        ``{` `            ``display(p);` `            ``p.clear();` `            ``return``;` `        ``}` `     `  `        ``// If given sum can be achieved after ignoring` `        ``// current element.` `        ``if` `(dp[i-``1``][sum])` `        ``{` `            ``// Create a new vector to store path` `            ``ArrayList b = ``new` `ArrayList<>();` `            ``b.addAll(p);` `            ``printSubsetsRec(arr, i-``1``, sum, b);` `        ``}` `     `  `        ``// If given sum can be achieved after considering` `        ``// current element.` `        ``if` `(sum >= arr[i] && dp[i-``1``][sum-arr[i]])` `        ``{` `            ``p.add(arr[i]);` `            ``printSubsetsRec(arr, i-``1``, sum-arr[i], p);` `        ``}` `    ``}` `     `  `    ``// Prints all subsets of arr[0..n-1] with sum 0.` `    ``static` `void` `printAllSubsets(``int` `arr[], ``int` `n, ``int` `sum)` `    ``{` `        ``if` `(n == ``0` `|| sum < ``0``)` `           ``return``;` `     `  `        ``// Sum 0 can always be achieved with 0 elements` `        ``dp = ``new` `boolean``[n][sum + ``1``];` `        ``for` `(``int` `i=``0``; i p = ``new` `ArrayList<>();` `        ``printSubsetsRec(arr, n-``1``, sum, p);` `    ``}` `    `  `    ``//Driver Program to test above functions` `    ``public` `static` `void` `main(String args[])` `    ``{` `        ``int` `arr[] = {``1``, ``2``, ``3``, ``4``, ``5``};` `        ``int` `n = arr.length;` `        ``int` `sum = ``10``;` `        ``printAllSubsets(arr, n, sum);` `    ``}` `}` `//This code is contributed by Sumit Ghosh`

Output:

```4 3 2 1
5 3 2
5 4 1```

Another Approach :
For each element in the array, first decide to take it or not in the subset. Define a function which will take care of all this. The function is called once in the main function. The static class fields are declared which will be operated by our function. At each call, the function checks for the condition of the fields. In our case, it checks if the current sum is equal to the given sum and accordingly increment the respective class field. If not, it makes function calls by taking all the case. So the number of function calls will be equal to the number of cases. So here, two calls are made – one by taking the element in the subset and increment the current sum and another by not taking the element.
Below is the implementation :

## C++

 `// C++ code to find the number of possible subset with given sum` `#include ` `using` `namespace` `std;`   `int` `n;` `int` `cnt;` `int` `sum;`   `void` `f(``int` `pat[], ``int` `i, ``int` `currSum)` `{` `    ``if` `(currSum == sum)` `    ``{` `        ``cnt++;` `        ``return``;` `    ``}`   `    ``if` `(currSum < sum && i < n)` `    ``{` `        ``f(pat, i + 1, currSum + pat[i]);` `        ``f(pat, i + 1, currSum);` `    ``}` `}`   `int` `main()` `{` `    ``cnt = 0;` `    ``n = 5;` `    ``sum = 10;`   `    ``int` `pat[] = {2, 3, 5, 6, 8, 10};` `    ``f(pat, 0, 0);`   `    ``cout << cnt << endl;` `    ``return` `0;` `}`   `/*This code is contributed by Nikhil Goswami (@nikhil070g) */`

## Java

 `// Java code to find the number of ` `// possible subset with given sum` `import` `java.util.*;` `import` `java.lang.*;` `import` `java.io.*;`   `class` `GFG {` `    `  `    ``static` `int` `count;` `    ``static` `int` `sum;` `    ``static` `int` `n;` `    `  `    ``// Driver code` `    ``public` `static` `void` `main (String[] args) {` `        ``count = ``0``;` `        ``n = ``5``;` `        ``sum = ``10``;`   `        ``int``[] pat = {``2``, ``3``, ``5``, ``6``, ``8``, ``10``};` `        `  `        ``f(pat, ``0``, ``0``);` `        `  `        ``System.out.println(count);` `    ``}` `    `  `    ``// Function to select or not the array element ` `    ``// to form a subset with given sum` `    ``static` `void` `f(``int``[] pat, ``int` `i, ``int` `currSum) {` `        ``if` `(currSum == sum) {` `            ``count++;` `            ``return``;` `        ``}` `        ``if` `(currSum < sum && i < n) {` `            ``f(pat, i+``1``, currSum + pat[i]);` `            ``f(pat, i+``1``, currSum);` `        ``}` `    ``}` `}`

## Python3

 `# Python code to find the number of ` `# possible subset with given sum` `def` `f(pat, i, currSum):` `    ``global` `cnt, n, ``sum` `    ``if` `(currSum ``=``=` `sum``):` `        ``cnt ``+``=` `1` `        ``return` `    ``if` `(currSum < ``sum` `and` `i < n):` `        ``f(pat, i ``+` `1``, currSum ``+` `pat[i])` `        ``f(pat, i ``+` `1``, currSum)`   `# driver code` `cnt ``=` `0` `n ``=` `5` `sum` `=` `10`   `pat ``=` `[``2``, ``3``, ``5``, ``6``, ``8``, ``10``]` `f(pat, ``0``, ``0``)`   `print``(cnt)`   `# This code is contributed by shinjanpatra`

## Javascript

 ``

Output

`2`

This article is contributed by Jas Kaur. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.