diff --git a/0-1Knapsack b/0-1Knapsack new file mode 100644 index 0000000..0519150 --- /dev/null +++ b/0-1Knapsack @@ -0,0 +1,96 @@ + #include + + using namespace std; + + int max(int a, int b) + { + return a > b ? a : b; + } + + void knapsackdp(int* wt, int* val, int size, int knapsack) + { + int dp[size][knapsack+1]; + //insert values + for(int i=0; i= wt[i]) + { + if(i>0) + { + dp[i][j] = max((val[i]+dp[i-1][j-wt[i]]), dp[i-1][j]); + } + else + { + dp[i][j] = val[i]; + } + } + else if(j < wt[i]) + { + dp[i][j] = dp[i-1][j]; + } + } + } + for(int i=0; i=0, j>=0; ) + { + cout<<"last arrya val: dp["< dp[i][j-1]) + { + cout<<"Item val: "<