File tree Expand file tree Collapse file tree 3 files changed +80
-0
lines changed Expand file tree Collapse file tree 3 files changed +80
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -41,6 +41,8 @@ Code | Test
41
41
[ Bubble Sort] ( https://github.com/kartikeysingh6/code_problems/blob/master/classical_algorithms/c++/bubblesort.cpp ) | Missing tests
42
42
[ Merge Sort] ( https://github.com/larissalages/code_problems/blob/master/classical_algorithms/c%2B%2B/mergesort.cpp ) | Missing tests
43
43
[ 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
44
46
45
47
# Codeforces
46
48
You can’t perform that action at this time.
0 commit comments