-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add khushal87 unbounded-knapsack-related-codes
- Loading branch information
Showing
11 changed files
with
352 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
//max number of ways to give a sum | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
int solve(int p[], int n, int cap) | ||
{ | ||
int t[n + 1][cap + 1]; | ||
for (int i = 0; i <= n; i++) | ||
{ | ||
for (int j = 0; j <= cap; j++) | ||
{ | ||
if (j == 0) | ||
{ | ||
t[i][j] = 1; | ||
} | ||
else if (i == 0 && j > 0) | ||
{ | ||
t[i][j] = 0; | ||
} | ||
else | ||
{ | ||
if (p[i - 1] <= j) | ||
{ | ||
t[i][j] = t[i][j - p[i - 1]] + t[i - 1][j]; | ||
} | ||
else | ||
{ | ||
t[i][j] = t[i - 1][j]; | ||
} | ||
} | ||
} | ||
} | ||
return t[n][cap]; | ||
} | ||
|
||
int main() | ||
{ | ||
int n; | ||
cin >> n; | ||
int p[n]; | ||
for (int i = 0; i < n; i++) | ||
{ | ||
cin >> p[i]; | ||
} | ||
int cap; | ||
cin >> cap; | ||
cout << solve(p, n, cap) << endl; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
//minimum number of coin to get a sum | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
int solve(int arr[], int n, int sum) | ||
{ | ||
int t[n + 1][sum + 1]; | ||
for (int i = 0; i <= sum; i++) | ||
{ | ||
t[0][i] = INT_MAX - 1; | ||
} | ||
for (int i = 1; i <= n; i++) | ||
{ | ||
t[i][0] = 0; | ||
} | ||
for (int i = 1; i <= sum; i++) | ||
{ | ||
if (i % arr[0] == 0) | ||
{ | ||
t[1][i] = i / arr[0]; | ||
} | ||
else | ||
{ | ||
t[1][i] = INT_MAX - 1; | ||
} | ||
} | ||
for (int i = 2; i <= n; i++) | ||
{ | ||
for (int j = 1; j <= sum; j++) | ||
{ | ||
if (arr[i - 1] <= j) | ||
{ | ||
t[i][j] = min(1 + t[i][j - arr[i - 1]], t[i - 1][j]); | ||
} | ||
else | ||
{ | ||
t[i][j] = t[i - 1][j]; | ||
} | ||
} | ||
} | ||
return t[n][sum]; | ||
} | ||
|
||
int main() | ||
{ | ||
int n; | ||
cin >> n; | ||
int p[n]; | ||
for (int i = 0; i < n; i++) | ||
{ | ||
cin >> p[i]; | ||
} | ||
int cap; | ||
cin >> cap; | ||
cout << solve(p, n, cap) << endl; | ||
} |
Binary file not shown.
53 changes: 53 additions & 0 deletions
53
Dynamic Programming/count_the_number_of_subset_with_given_sum.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
// sum(s1)-sum(s2)=diff | ||
// sum(s1)+sum(s2)=sumOfArray | ||
// -> sum(s1)= diff+sumOfArray/2; | ||
int solve(int p[], int n, int cap) | ||
{ | ||
int t[n + 1][cap + 1]; | ||
for (int i = 0; i <= n; i++) | ||
{ | ||
for (int j = 0; j <= cap; j++) | ||
{ | ||
if (j == 0) | ||
{ | ||
t[i][j] = 1; | ||
} | ||
else if (i == 0 && j > 0) | ||
{ | ||
t[i][j] = 0; | ||
} | ||
else | ||
{ | ||
if (p[i - 1] <= j) | ||
{ | ||
t[i][j] = t[i - 1][j - p[i - 1]] + t[i - 1][j]; | ||
} | ||
else | ||
{ | ||
t[i][j] = t[i - 1][j]; | ||
} | ||
} | ||
} | ||
} | ||
return t[n][cap]; | ||
} | ||
|
||
int main() | ||
{ | ||
int n; | ||
cin >> n; | ||
int p[n]; | ||
int sum = 0; | ||
for (int i = 0; i < n; i++) | ||
{ | ||
cin >> p[i]; | ||
sum += p[i]; | ||
} | ||
int diff; | ||
cin >> diff; | ||
int passedSum = diff + sum / 2; | ||
cout << solve(p, n, passedSum) << endl; | ||
} |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
int solve(int arr[], int n) | ||
{ | ||
int sum = 0; | ||
for (int i = 0; i < n; i++) | ||
sum += arr[i]; | ||
bool t[n + 1][sum + 1]; | ||
for (int i = 0; i <= n; i++) | ||
{ | ||
for (int j = 0; j <= sum; j++) | ||
{ | ||
if (j == 0) | ||
{ | ||
t[i][j] = true; | ||
} | ||
else if (i == 0 && j > 0) | ||
{ | ||
t[i][j] = false; | ||
} | ||
else | ||
{ | ||
if (arr[i - 1] <= j) | ||
{ | ||
t[i][j] = t[i - 1][j - arr[i - 1]] || t[i - 1][j]; | ||
} | ||
else | ||
{ | ||
t[i][j] = t[i - 1][j]; | ||
} | ||
} | ||
} | ||
} | ||
vector<int> temp; | ||
for (int i = 0; i <= sum / 2; i++) | ||
{ | ||
if (t[n][i]) | ||
{ | ||
temp.push_back(i); | ||
} | ||
} | ||
int minm = INT_MAX; | ||
for (auto j : temp) | ||
{ | ||
minm = min(minm, sum - 2 * j); | ||
} | ||
return minm; | ||
} | ||
|
||
int main() | ||
{ | ||
int n; | ||
cin >> n; | ||
int arr[n]; | ||
for (int i = 0; i < n; i++) | ||
{ | ||
cin >> arr[i]; | ||
} | ||
cout << solve(arr, n) << endl; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
//same as unbounded cpp | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
int solve(int p[], int len[], int n, int lenRod) | ||
{ | ||
int t[n + 1][lenRod + 1]; | ||
for (int i = 0; i <= n; i++) | ||
{ | ||
for (int j = 0; j <= lenRod; j++) | ||
{ | ||
if (i == 0 || j == 0) | ||
{ | ||
t[i][j] = 0; | ||
} | ||
else if (len[i - 1] <= j) | ||
{ | ||
t[i][j] = max(p[i - 1] + t[i][j - len[i - 1]], t[i - 1][j]); | ||
} | ||
else | ||
{ | ||
t[i][j] = t[i - 1][j]; | ||
} | ||
} | ||
} | ||
return t[n][lenRod]; | ||
} | ||
|
||
int main() | ||
{ | ||
int n; | ||
cin >> n; | ||
int p[n], len[n]; | ||
for (int i = 0; i < n; i++) | ||
{ | ||
cin >> p[i] >> len[i]; | ||
} | ||
int lenRod; | ||
cin >> lenRod; | ||
cout << solve(p, len, n, lenRod) << endl; | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
// sum(s1)-sum(s2)=diff | ||
// sum(s1)+sum(s2)=sumOfArray | ||
// -> sum(s1)= diff+sumOfArray/2; | ||
int solve(int p[], int n, int cap) | ||
{ | ||
int t[n + 1][cap + 1]; | ||
for (int i = 0; i <= n; i++) | ||
{ | ||
for (int j = 0; j <= cap; j++) | ||
{ | ||
if (j == 0) | ||
{ | ||
t[i][j] = 1; | ||
} | ||
else if (i == 0 && j > 0) | ||
{ | ||
t[i][j] = 0; | ||
} | ||
else | ||
{ | ||
if (p[i - 1] <= j) | ||
{ | ||
t[i][j] = t[i - 1][j - p[i - 1]] + t[i - 1][j]; | ||
} | ||
else | ||
{ | ||
t[i][j] = t[i - 1][j]; | ||
} | ||
} | ||
} | ||
} | ||
return t[n][cap]; | ||
} | ||
|
||
int main() | ||
{ | ||
int n; | ||
cin >> n; | ||
int p[n]; | ||
int sum = 0; | ||
for (int i = 0; i < n; i++) | ||
{ | ||
cin >> p[i]; | ||
sum += p[i]; | ||
} | ||
int diff; | ||
cin >> diff; | ||
int passedSum = diff + sum / 2; | ||
cout << solve(p, n, passedSum) << endl; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
int solve(int p[], int w[], int n, int cap) | ||
{ | ||
int t[n + 1][cap + 1]; | ||
for (int i = 0; i <= n; i++) | ||
{ | ||
for (int j = 0; j <= cap; j++) | ||
{ | ||
if (i == 0 || j == 0) | ||
{ | ||
t[i][j] = 0; | ||
} | ||
else if (w[i - 1] <= j) | ||
{ | ||
t[i][j] = max(p[i - 1] + t[i][j - w[i - 1]], t[i - 1][j]); | ||
} | ||
else | ||
{ | ||
t[i][j] = t[i - 1][j]; | ||
} | ||
} | ||
} | ||
return t[n][cap]; | ||
} | ||
|
||
int main() | ||
{ | ||
int n; | ||
cin >> n; | ||
int p[n], w[n]; | ||
for (int i = 0; i < n; i++) | ||
{ | ||
cin >> p[i] >> w[i]; | ||
} | ||
int cap; | ||
cin >> cap; | ||
cout << solve(p, w, n, cap) << endl; | ||
} |