# Subset Sum Problem in O(sum) space

Given an array of non-negative integers and a value sum, determine if there is a subset of the given set with sum equal to given sum.

Examples:

```Input : arr[] = {4, 1, 10, 12, 5, 2},
sum = 9
Output : TRUE
{4, 5} is a subset with sum 9.

Input : arr[] = {1, 8, 2, 5},
sum = 4
Output : FALSE
There exists no subset with sum 4.
```

## Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.

We have discussed a Dynamic Programming based solution in below post.
Dynamic Programming | Set 25 (Subset Sum Problem)

The solution discussed above requires O(n * sum) space and O(n * sum) time. We can optimize space. We create a boolean 2D array subset[sum+1]. Using bottom up manner we can fill up this table. The idea behind using 2 in “subset[sum+1]” is that for filling a row only the values from previous row is required. So alternate rows are used either making the first one as current and second as previous or the first as previous and second as current.

## C++

 `// Returns true if there exists a subset ` `// with given sum in arr[] ` `#include ` `#include ` ` `  `bool` `isSubsetSum(``int` `arr[], ``int` `n, ``int` `sum) ` `{ ` `    ``// The value of subset[i%2][j] will be true  ` `    ``// if there exists a subset of sum j in  ` `    ``// arr[0, 1, ...., i-1] ` `    ``bool` `subset[sum + 1]; ` ` `  `    ``for` `(``int` `i = 0; i <= n; i++) { ` `        ``for` `(``int` `j = 0; j <= sum; j++) { ` ` `  `            ``// A subset with sum 0 is always possible  ` `            ``if` `(j == 0) ` `                ``subset[i % 2][j] = ``true``;  ` ` `  `            ``// If there exists no element no sum  ` `            ``// is possible  ` `            ``else` `if` `(i == 0) ` `                ``subset[i % 2][j] = ``false``;  ` `            ``else` `if` `(arr[i - 1] <= j) ` `                ``subset[i % 2][j] = subset[(i + 1) % 2] ` `             ``[j - arr[i - 1]] || subset[(i + 1) % 2][j]; ` `            ``else` `                ``subset[i % 2][j] = subset[(i + 1) % 2][j]; ` `        ``} ` `    ``} ` ` `  `    ``return` `subset[n % 2][sum]; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 6, 2, 5 }; ` `    ``int` `sum = 7; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` `    ``if` `(isSubsetSum(arr, n, sum) == ``true``) ` `        ``printf``(``"There exists a subset with given sum"``); ` `    ``else` `        ``printf``(``"No subset exists with given sum"``); ` `    ``return` `0; ` `} `

## Java

 `// Java Program to get a subset with a  ` `// with a sum provided by the user ` `public` `class` `Subset_sum { ` `     `  `    ``// Returns true if there exists a subset ` `    ``// with given sum in arr[] ` `    ``static` `boolean` `isSubsetSum(``int` `arr[], ``int` `n, ``int` `sum) ` `    ``{ ` `        ``// The value of subset[i%2][j] will be true  ` `        ``// if there exists a subset of sum j in  ` `        ``// arr[0, 1, ...., i-1] ` `        ``boolean` `subset[][] = ``new` `boolean``[``2``][sum + ``1``]; ` `      `  `        ``for` `(``int` `i = ``0``; i <= n; i++) { ` `            ``for` `(``int` `j = ``0``; j <= sum; j++) { ` `      `  `                ``// A subset with sum 0 is always possible  ` `                ``if` `(j == ``0``) ` `                    ``subset[i % ``2``][j] = ``true``;  ` `      `  `                ``// If there exists no element no sum  ` `                ``// is possible  ` `                ``else` `if` `(i == ``0``) ` `                    ``subset[i % ``2``][j] = ``false``;  ` `                ``else` `if` `(arr[i - ``1``] <= j) ` `                    ``subset[i % ``2``][j] = subset[(i + ``1``) % ``2``] ` `                 ``[j - arr[i - ``1``]] || subset[(i + ``1``) % ``2``][j]; ` `                ``else` `                    ``subset[i % ``2``][j] = subset[(i + ``1``) % ``2``][j]; ` `            ``} ` `        ``} ` `      `  `        ``return` `subset[n % ``2``][sum]; ` `    ``} ` `      `  `    ``// Driver code ` `    ``public` `static` `void` `main(String args[]) ` `    ``{ ` `        ``int` `arr[] = { ``1``, ``2``, ``5` `}; ` `        ``int` `sum = ``7``; ` `        ``int` `n = arr.length; ` `        ``if` `(isSubsetSum(arr, n, sum) == ``true``) ` `            ``System.out.println(``"There exists a subset with"` `+  ` `                                              ``"given sum"``); ` `        ``else` `            ``System.out.println(``"No subset exists with"` `+  ` `                                           ``"given sum"``); ` `    ``} ` `} ` `// This code is contributed by Sumit Ghosh `

## Python

 `# Returns true if there exists a subset ` `# with given sum in arr[] ` ` `  `def` `isSubsetSum(arr, n, ``sum``): ` `    `  `    ``# The value of subset[i%2][j] will be true  ` `    ``# if there exists a subset of sum j in  ` `    ``# arr[0, 1, ...., i-1] ` `    ``subset ``=` `[ [``False` `for` `j ``in` `range``(``sum` `+` `1``)] ``for` `i ``in` `range``(``3``) ] ` `  `  `    ``for` `i ``in` `range``(n ``+` `1``): ` `        ``for` `j ``in` `range``(``sum` `+` `1``): ` `            ``# A subset with sum 0 is always possible  ` `            ``if` `(j ``=``=` `0``): ` `                ``subset[i ``%` `2``][j] ``=` `True` `  `  `            ``# If there exists no element no sum  ` `            ``# is possible  ` `            ``elif` `(i ``=``=` `0``): ` `                ``subset[i ``%` `2``][j] ``=` `False` `            ``elif` `(arr[i ``-` `1``] <``=` `j): ` `                ``subset[i ``%` `2``][j] ``=` `subset[(i ``+` `1``) ``%` `2``][j ``-` `arr[i ``-` `1``]] ``or` `subset[(i ``+` `1``)  ` `                                                                               ``%` `2``][j] ` `            ``else``: ` `                ``subset[i ``%` `2``][j] ``=` `subset[(i ``+` `1``) ``%` `2``][j] ` `                 `  `    ``return` `subset[n ``%` `2``][``sum``] ` `  `  `# Driver code ` `arr ``=` `[ ``6``, ``2``, ``5` `] ` `sum` `=` `7` `n ``=` `len``(arr) ` `if` `(isSubsetSum(arr, n, ``sum``) ``=``=` `True``): ` `    ``print` `(``"There exists a subset with given sum"``) ` `else``: ` `    ``print` `(``"No subset exists with given sum"``) ` `     `  `# This code is contributed by Sachin Bisht `

## C#

 `// C# Program to get a subset with a  ` `// with a sum provided by the user  ` ` `  `using` `System; ` ` `  `public` `class` `Subset_sum {  ` `     `  `    ``// Returns true if there exists a subset  ` `    ``// with given sum in arr[]  ` `    ``static` `bool` `isSubsetSum(``int` `[]arr, ``int` `n, ``int` `sum)  ` `    ``{  ` `        ``// The value of subset[i%2][j] will be true  ` `        ``// if there exists a subset of sum j in  ` `        ``// arr[0, 1, ...., i-1]  ` `        ``bool` `[,]subset = ``new` `bool``[2,sum + 1];  ` `     `  `        ``for` `(``int` `i = 0; i <= n; i++) {  ` `            ``for` `(``int` `j = 0; j <= sum; j++) {  ` `     `  `                ``// A subset with sum 0 is always possible  ` `                ``if` `(j == 0)  ` `                    ``subset[i % 2,j] = ``true``;  ` `     `  `                ``// If there exists no element no sum  ` `                ``// is possible  ` `                ``else` `if` `(i == 0)  ` `                    ``subset[i % 2,j] = ``false``;  ` `                ``else` `if` `(arr[i - 1] <= j)  ` `                    ``subset[i % 2,j] = subset[(i + 1) % 2,j - arr[i - 1]] || subset[(i + 1) % 2,j];  ` `                ``else` `                    ``subset[i % 2,j] = subset[(i + 1) % 2,j];  ` `            ``}  ` `        ``}  ` `     `  `        ``return` `subset[n % 2,sum];  ` `    ``}  ` `     `  `    ``// Driver code  ` `    ``public` `static` `void` `Main()  ` `    ``{  ` `        ``int` `[]arr = { 1, 2, 5 };  ` `        ``int` `sum = 7;  ` `        ``int` `n = arr.Length;  ` `        ``if` `(isSubsetSum(arr, n, sum) == ``true``)  ` `            ``Console.WriteLine(``"There exists a subset with"` `+ ` `                                         ``"given sum"``);  ` `        ``else` `            ``Console.WriteLine(``"No subset exists with"` `+  ` `                                        ``"given sum"``);  ` `    ``}  ` `}  ` `// This code is contributed by Ryuga  `

## PHP

 ` `

Output:

```There exists a subset with given sum
```

This article is contributed by Neelesh (Neelesh_Sinha). 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.