Skip to content

Commit 818e380

Browse files
authored
Merge pull request #178 from ErR0rpj/master
0/1 Knapsack Problem (Dynamic Programming)
2 parents d135612 + c40ba46 commit 818e380

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//This is a code for 01 Knapsack problem.
2+
//input for number of items, capacity of the bag (total weight the bag can take).
3+
//Input for values of each items and their weights.
4+
//Output is maximum possible value the bag can take.
5+
#include <bits/stdc++.h>
6+
using namespace std;
7+
8+
int main() {
9+
10+
int Number_of_items, Capacity;
11+
cout<<"Enter total number of items and capacity:\n";
12+
cin>>Number_of_items>>Capacity;
13+
14+
vector<int> values (Number_of_items+1);
15+
vector<int> weights (Number_of_items+1);
16+
values[0] = 0;
17+
weights[0] = 0;
18+
19+
//DP array.
20+
vector<vector<int>> dp (Number_of_items+1, vector<int>(Capacity+1, 0));
21+
22+
//Input for values
23+
cout<<"Enter the values of each items:\n";
24+
for(int i=1; i<Number_of_items+1; i++){
25+
cin>>values[i];
26+
}
27+
28+
//Input for weights of the items with respect to values.
29+
cout<<"Enter the values of each weights:\n";
30+
for(int i=1; i<Number_of_items+1; i++){
31+
cin>>weights[i];
32+
}
33+
34+
//Main algo for 0/1 Knapsack.
35+
for(int i=1; i<Number_of_items+1; i++){
36+
int wnow = weights[i];
37+
int vnow = values[i];
38+
for(int j=1; j<Capacity+1; j++){
39+
if(j < wnow){
40+
dp[i][j] = dp[i-1][j];
41+
}
42+
else{
43+
dp[i][j] = max(dp[i-1][j], values[i] + dp[i-1][j - weights[i]]);
44+
}
45+
}
46+
}
47+
48+
cout<<"The maximum value the bag can store with the given capacity is: ";
49+
cout<<dp[Number_of_items][Capacity]<<"\n";
50+
51+
return 0;
52+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//Given an array of intergers. Find the contiguous sub-array with maximum sum.
2+
3+
#include <bits/stdc++.h>
4+
using namespace std;
5+
6+
int main() {
7+
cout<<"Enter the number of elements for array:\n";
8+
int n;
9+
cin>>n;
10+
11+
vector<int>v(n);
12+
cout<<"Enter the values of the array:\n";
13+
for(int i=0;i<n;i++){
14+
cin>>v[i];
15+
}
16+
int ans = INT_MIN;
17+
int sum = 0;
18+
for(int i=0;i<n;i++){
19+
sum = max(v[i], sum + v[i]);
20+
ans = max(ans, sum);
21+
}
22+
23+
cout<<"The maximum sum of contigous array is: ";
24+
cout<<ans<<"\n";
25+
return 0;
26+
}

readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ Code | Test
4141
[Bubble Sort](https://github.com/kartikeysingh6/code_problems/blob/master/classical_algorithms/c++/bubblesort.cpp) | Missing tests
4242
[Merge Sort](https://github.com/larissalages/code_problems/blob/master/classical_algorithms/c%2B%2B/mergesort.cpp) | Missing tests
4343
[Quick Sort](https://github.com/kartikeysingh6/code_problems/blob/master/classical_algorithms/c++/quicksort.cpp) | Missing tests
44+
[0/1 Knapsack Problem](https://github.com/ErR0rpj/code_problems/blob/master/classical_algorithms/c%2B%2B/01_knapsack_problem.cpp) | Missing tests
45+
[Kadane's Algorithm](https://github.com/ErR0rpj/code_problems/blob/master/classical_algorithms/c%2B%2B/Kadane_Algorithm.cpp) | Missing tests
4446

4547
# Codeforces
4648

0 commit comments

Comments
 (0)