# A Space Optimized DP solution for 0-1 Knapsack Problem

Given the weights and values of n items, put these items in a knapsack of capacity W to get the maximum total value in the knapsack. In other words, given two integer arrays val[0..n-1] and wt[0..n-1] which represent values and weights associated with n items respectively. Also given an integer W which represents knapsack capacity, find out the maximum value subset of val[] such that sum of the weights of this subset is smaller than or equal to W. We cannot break an item, either pick the complete item or don’t pick it (0-1 property).

Here W <= 2000000 and n <= 500

Examples:

```Input : W = 10, n = 3
val[] = {7, 8, 4}
wt[] = {3, 8, 6}
Output: 11
We get maximum value by picking items of 3 KG and 6 KG.
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

We have discussed a Dynamic Programming based solution here. In the previous solution, we used a n * W matrix. We can reduce the used extra space. The idea behind the optimization is, to compute mat[i][j], we only need solution of previous row. In 0-1 Knapsack Problem if we are currently on mat[i][j] and we include ith element then we move j-wt[i] steps back in previous row and if we exclude the current element we move on jth column in previous row. So here we can observe that at a time we are working only with 2 consecutive rows.
In below solution, we create a matrix of size 2*W. If n is odd, then the final answer will be at mat[W] and if n is even then the final answer will be at mat[W] because index starts from 0.

## C++

 `// C++ program of a space optimized DP solution for ` `// 0-1 knapsack problem. ` `#include ` `using` `namespace` `std; ` ` `  `// val[] is for storing maximum profit for each weight ` `// wt[] is for storing weights ` `// n number of item ` `// W maximum capacity of bag ` `// mat[W+1] to store final result ` `int` `KnapSack(``int` `val[], ``int` `wt[], ``int` `n, ``int` `W) ` `{ ` `    ``// matrix to store final result ` `    ``int` `mat[W+1]; ` `    ``memset``(mat, 0, ``sizeof``(mat)); ` ` `  `    ``// iterate through all items ` `    ``int` `i = 0; ` `    ``while` `(i < n) ``// one by one traverse each element ` `    ``{ ` `        ``int` `j = 0; ``// traverse all weights j <= W ` ` `  `        ``// if i is odd that mean till now we have odd ` `        ``// number of elements so we store result in 1th ` `        ``// indexed row ` `        ``if` `(i%2!=0) ` `        ``{ ` `            ``while` `(++j <= W) ``// check for each value ` `            ``{ ` `                ``if` `(wt[i] <= j) ``// include element ` `                    ``mat[j] = max(val[i] + mat[j-wt[i]], ` `                                    ``mat[j] ); ` `                ``else`           `// exclude element ` `                    ``mat[j] = mat[j]; ` `            ``} ` ` `  `        ``} ` ` `  `        ``// if i is even that mean till now we have even number ` `        ``// of elements so we store result in 0th indexed row ` `        ``else` `        ``{ ` `            ``while``(++j <= W) ` `            ``{ ` `                ``if` `(wt[i] <= j) ` `                    ``mat[j] = max(val[i] + mat[j-wt[i]], ` `                                     ``mat[j]); ` `                ``else` `                    ``mat[j] = mat[j]; ` `            ``} ` `        ``} ` `        ``i++; ` `    ``} ` ` `  `    ``// Return mat[W] if n is odd, else mat[W] ` `    ``return` `(n%2 != 0)? mat[W] : mat[W]; ` `} ` ` `  `// Driver program to test the cases ` `int` `main() ` `{ ` `    ``int` `val[] = {7, 8, 4}, wt[] = {3, 8, 6}, W = 10, n = 3; ` `    ``cout << KnapSack(val, wt, n, W) << endl; ` `    ``return` `0; ` `} `

## Python3

 `# Python program of a space  ` `# optimized DP solution for  ` `# 0-1 knapsack problem.  ` ` `  `# val[] is for storing maximum ` `# profit for each weight  ` `# wt[] is for storing weights  ` `# n number of item  ` `# W maximum capacity of bag  ` `# mat[W+1] to store final result  ` ` `  `def` `KnapSack(val, wt, n, W): ` `     `  `    ``# matrix to store final result  ` `    ``mat ``=` `[[``0` `for` `i ``in` `range``(W ``+` `1``)]  ` `              ``for` `i ``in` `range``(``2``)] ` `    ``# iterate through all items  ` `    ``i ``=` `0` `    ``while` `i < n: ``# one by one traverse  ` `                 ``# each element  ` `        ``j ``=` `0` `# traverse all weights j <= W ` `         `  `        ``# if i is odd that mean till  ` `        ``# now we have odd number of  ` `        ``# elements so we store result   ` `        ``# in 1th indexed row  ` `        ``if` `i ``%` `2` `=``=` `0``: ` `            ``while` `j < W: ``# check for each value  ` `                ``j ``+``=` `1` `                ``if` `wt[i] <``=` `j: ``# include element  ` `                    ``mat[``1``][j] ``=` `max``(val[i] ``+` `mat[``0``][j ``-`  `                                     ``wt[i]], mat[``0``][j]) ` `                ``else``: ``# exclude element ` `                    ``mat[``1``][j] ``=` `mat[``0``][j] ` `                     `  `        ``# if i is even that mean till  ` `        ``# now we have even number  ` `        ``# of elements so we store  ` `        ``# result in 0th indexed row  ` `        ``else``: ` `            ``while` `j < W: ` `                ``j ``+``=` `1` `                ``if` `wt[i] <``=` `j: ` `                    ``mat[``0``][j] ``=` `max``(val[i] ``+` `mat[``1``][j ``-`  `                                     ``wt[i]], mat[``1``][j]) ` `                ``else``: ` `                    ``mat[``0``][j] ``=` `mat[``1``][j] ` `        ``i ``+``=` `1` `    ``# Return mat[W] if n is  ` `    ``# odd, else mat[W]  ` `    ``if` `n ``%` `2` `=``=` `0``: ` `        ``return` `mat[``0``][W] ` `    ``else``: ` `        ``return` `mat[``1``][W] ` ` `  `# Driver code ` `val ``=` `[``7``, ``8``, ``4``] ` `wt ``=` `[``3``, ``8``, ``6``]  ` `W ``=` `10` `n ``=` `3` `print``(KnapSack(val, wt, n, W)) ` ` `  `# This code is contributed ` `# by sahilshelangia `

/div>

## PHP

 ` `

## JAVA

// Java program of a space optimized DP solution for
// 0-1 knapsack problem.

class GFG {

// val[] is for storing maximum profit for each weight
// wt[] is for storing weights
// n number of item
// W maximum capacity of bag
// mat[W+1] to store final result
static int KnapSack(int val[], int wt[], int n, int W) {
// matrix to store final result
int mat[][] = new int[W + 1];

// iterate through all items
int i = 0;
while (i < n) // one by one traverse each element { int j = 0; // traverse all weights j <= W // if i is odd that mean till now we have odd // number of elements so we store result in 1th // indexed row if (i % 2 != 0) { while (++j <= W) // check for each value { if (wt[i] <= j) // include element { mat[j] = Math.max(val[i] + mat[j - wt[i]], mat[j]); } else // exclude element { mat[j] = mat[j]; } } } // if i is even that mean till now we have even number // of elements so we store result in 0th indexed row else { while (++j <= W) { if (wt[i] <= j) { mat[j] = Math.max(val[i] + mat[j - wt[i]], mat[j]); } else { mat[j] = mat[j]; } } } i++; } // Return mat[W] if n is odd, else mat[W] return (n % 2 != 0) ? mat[W] : mat[W]; } // Driver program to test the cases public static void main(String[] args) { int val[] = {7, 8, 4}, wt[] = {3, 8, 6}, W = 10, n = 3; System.out.println(KnapSack(val, wt, n, W)); } } /* This code contributed by PrinciRaj1992 */ [tabbyending] Output:

```11
```

Time Complexity: O(n * W)
Auxiliary Space: O(W)

Here is an optimized code contributed by Gaurav Mamgain

## C++

 `// C++ program of a space optimized DP solution for ` `// 0-1 knapsack problem. ` `#include ` `using` `namespace` `std; ` ` `  `// val[] is for storing maximum profit for each weight ` `// wt[] is for storing weights ` `// n number of item ` `// W maximum capacity of bag ` `// dp[W+1] to store final result ` `int` `KnapSack(``int` `val[], ``int` `wt[], ``int` `n, ``int` `W) ` `{ ` `    ``// array to store final result ` `    ``//dp[i] stores the profit with KnapSack capacity "i" ` `    ``int` `dp[W+1]; ` `     `  `    ``//initially profit with 0 to W KnapSack capacity is 0 ` `    ``memset``(dp, 0, ``sizeof``(dp)); ` ` `  `    ``// iterate through all items ` `    ``for``(``int` `i=0; i < n; i++)  ` `        ``//traverse dp array from right to left ` `        ``for``(``int` `j=W; j>=wt[i]; j--) ` `            ``dp[j] = max(dp[j] , val[i] + dp[j-wt[i]]); ` `    ``/*above line finds out maximum of  dp[j](excluding ith element value) ` `      ``and val[i] + dp[j-wt[i]] (including ith element value and the ` `      ``profit with "KnapSack capacity - ith element weight") */`     `    ``return` `dp[W]; ` `} ` ` `  `// Driver program to test the cases ` `int` `main() ` `{ ` `    ``int` `val[] = {7, 8, 4}, wt[] = {3, 8, 6}, W = 10, n = 3; ` `    ``cout << KnapSack(val, wt, n, W) << endl; ` `    ``return` `0; ` `} ` ` `  `// This code is contributed by Gaurav Mamgain `

Output:

```11
```