Skip to content

Commit 4c178c3

Browse files
authored
Add files via upload
1 parent 3fd1976 commit 4c178c3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+2010
-0
lines changed

01_Recursive_Knapsack.cpp

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int Knapsack(int wt[], int val[], int W, int n) {
5+
// every recursive solution will have a base condition
6+
// for base condition we need to think of the smallest valid input that we can pass
7+
// array size can be atleast 0 || min weight can be 0 but not negetive;
8+
if (n == 0 || W == 0)
9+
return 0;
10+
11+
// these are the choices we are having
12+
if (wt[n - 1] <= W) {
13+
return max(val[n - 1] + Knapsack(wt, val, W - wt[n - 1], n - 1), Knapsack(wt, val, W, n - 1));
14+
}
15+
else if (wt[n - 1] > W) // if the weight is greater then the required weight there is no sence for taking that value.
16+
return Knapsack(wt, val, W, n - 1); // return as it is by redusing the size of array
17+
else
18+
return -1;
19+
}
20+
21+
int main() {
22+
int n,W;
23+
cin >> n; // number of items
24+
int val[n], wt[n]; // values and weights of array
25+
for (int i = 0; i < n; i++)
26+
cin >> wt[i];
27+
for (int i = 0; i < n; i++)
28+
cin >> val[i];
29+
30+
cin >> W; // Knapsack capacity
31+
32+
cout << Knapsack(wt, val, W, n) << endl;
33+
return 0;
34+
}
35+
// T(N) = 2T(N-1) + O(1), which is simplified to O(2^N).

02_Knapsack_memoization.cpp

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
const int D = 1000; // DP - matrix dimension
5+
6+
int t[D][D]; // DP matrix
7+
8+
int Knapsack(int wt[], int val[], int W, int n) {
9+
// base case
10+
if (n == 0 || W == 0)
11+
return 0;
12+
13+
// if already calculated
14+
if (t[n][W] != -1)
15+
return t[n][W];
16+
17+
// else calculate
18+
else {
19+
if (wt[n - 1] <= W)
20+
t[n][W] = max(val[n - 1] + Knapsack(wt, val, W - wt[n - 1], n - 1),Knapsack(wt, val, W, n - 1));
21+
else if (wt[n - 1] > W)
22+
t[n][W] = Knapsack(wt, val, W, n - 1);
23+
24+
return t[n][W];
25+
}
26+
}
27+
28+
signed main() {
29+
int n; cin >> n; // number of items
30+
int val[n], wt[n]; // values and wts array
31+
for (int i = 0; i < n; i++)
32+
cin >> wt[i];
33+
for (int i = 0; i < n; i++)
34+
cin >> val[i];
35+
int W;
36+
cin >> W; // capacity
37+
38+
// matrix initialization
39+
for (int i = 0; i <= n; i++)
40+
for (int j = 0; j <= W; j++)
41+
t[i][j] = -1; // initialize matrix with -1
42+
43+
cout << Knapsack(wt, val, W, n) << endl;
44+
return 0;
45+
}

03_Knapsack_BottomUp.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Question Link :- https://www.geeksforgeeks.org/problems/0-1-knapsack-problem0945/1
2+
// 0 - 1 Knapsack Problem
3+
4+
// Notes - https://www.geeksforgeeks.org/0-1-knapsack-problem-dp-10/
5+
6+
// T.C = O(N*W), where ‘N’ is the number of weight element and ‘W’ is capacity. As for every weight
7+
// element we traverse through all weight capacities 1<=w<=W.
8+
// S.C = O(N*W) (2-D array of size ‘N*W’)
9+
10+
11+
class Solution {
12+
public:
13+
int knapSack(int W, int wt[], int val[], int n) {
14+
int t[n + 1][W + 1];
15+
for (int i = 0; i<n+1; i++) {
16+
for (int j = 0; j<W+1; j++) {
17+
if (i == 0 || j == 0) {// initialization
18+
t[i][j] = 0;
19+
}
20+
}
21+
}
22+
for (int i = 1; i<n+1; i++) {
23+
for (int j = 1; j<W+1; j++) {
24+
if (wt[i - 1] <= j) { // current wt can fit in bag
25+
t[i][j] = max(val[i - 1] + t[i - 1][j - wt[i - 1]], t[i - 1][j]);
26+
}
27+
else if (wt[i - 1] > j) { // current wt doesn't fit in bag
28+
t[i][j] = t[i - 1][j]; // move to next
29+
}
30+
}
31+
}
32+
return t[n][W];
33+
}
34+
};

04_SubsetSum.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Question Link :- https://www.geeksforgeeks.org/problems/subset-sum-problem-1611555638/1
2+
// Subset Sum Problem
3+
4+
// T.C = O(sum*n)
5+
// S.C = O(sum*n)
6+
class Solution {
7+
public:
8+
bool isSubsetSum(vector<int>arr, int sum) {
9+
int n = arr.size();
10+
bool t[n + 1][sum + 1];
11+
// initialization
12+
for (int i=0; i<n+1; i++) { // i -> size of the array
13+
for (int j=0; j<sum+1; j++) { // j -> target sum (subset sum)
14+
if (i == 0) {
15+
t[i][j] = false;
16+
}
17+
if (j == 0) {
18+
t[i][j] = true;
19+
}
20+
}
21+
}
22+
for (int i = 1; i<n+1; i++) {
23+
for (int j = 1; j<sum+1; j++) {
24+
if (arr[i - 1] <= j) { // if element of arr is smaller than the target sum
25+
t[i][j] = t[i - 1][j - arr[i - 1]] || t[i - 1][j]; // either take or(||) do not take
26+
}
27+
else { // if element of arr is greater than the target sum
28+
t[i][j] = t[i - 1][j];
29+
}
30+
}
31+
}
32+
return t[n][sum]; // at last return T/F
33+
}
34+
};

05_EqualSumPartition.cpp

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Question Link :- https://leetcode.com/problems/partition-equal-subset-sum/
2+
// Partition Equal Subset Sum
3+
4+
// T.C = O(sum*n)
5+
// S.C = O(sum)
6+
class Solution {
7+
public:
8+
bool isSubsetPossible(vector<int>& arr, int n, int sum) {
9+
bool t[n + 1][sum + 1]; // DP - matrix
10+
// initialization
11+
for (int i = 0; i<n+1; i++) { // i -> size of array
12+
for (int j = 0; j<sum+1; j++) { // j -> target sum (subset sum)
13+
// when array(i) is empty than there is no meaning of sum of elements so return false
14+
if (i == 0) {
15+
t[i][j] = false;
16+
}
17+
// when sum(j) is zero and there is always a chance of empty subset so return it as true;
18+
if (j == 0) {
19+
t[i][j] = true;
20+
}
21+
}
22+
}
23+
for (int i = 1; i <n+1; i++) {
24+
for (int j = 1; j <sum+1; j++) {
25+
if (arr[i - 1] <= j) {
26+
// after taking and element substract from the (sum) i.e -> in {3,8} 3 is taken then we want 11-3=8in the array
27+
t[i][j] = t[i - 1][j - arr[i - 1]] || t[i - 1][j]; // either take or(||) do not take
28+
} else {// if sum is less than array size just leave and increment
29+
t[i][j] = t[i - 1][j];
30+
}
31+
}
32+
}
33+
return t[n][sum]; // at last return T/F
34+
}
35+
36+
bool canPartition(vector<int>& arr) {
37+
int n = arr.size();
38+
int sum = 0;
39+
for (int i = 0; i < n; i++) {
40+
sum += arr[i];
41+
}
42+
if (sum % 2 != 0) { // if sum is odd --> not possible to make equal partitions
43+
return false;
44+
}
45+
return isSubsetPossible(arr, n, sum / 2);
46+
}
47+
};

06_CountOfSubset_with_given_sum.cpp

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Question Link :- https://www.geeksforgeeks.org/problems/perfect-sum-problem5633/1
2+
// Perfect Sum Problem
3+
4+
5+
// Brute Force
6+
// generating all the possible subsets and then checking whether the subset has the required sum.
7+
8+
9+
// Tabulation (Bottom-Up)
10+
// T.C = O(sum*n)
11+
// S.C = O(sum*n)
12+
class Solution {
13+
public:
14+
int mod = 1e9 + 7;
15+
int perfectSum(int arr[], int n, int sum) {
16+
// vector<vector<int>> t(n + 1, vector<int>(sum + 1, 0));
17+
int t[n + 1][sum + 1];
18+
for (int i = 0; i<n+1; i++) { // i -> size of array
19+
for (int j = 0; j<sum+1; j++) { // j -> target sum (subset sum)
20+
if (i == 0) {
21+
t[i][j] = 0;
22+
}
23+
if (j == 0) {
24+
t[i][j] = 1;
25+
}
26+
}
27+
}
28+
29+
for (int i = 1; i <n+1; i++) {
30+
for (int j = 0; j < sum+1; j++) { // NOTE :- here j started from 0
31+
if (arr[i - 1] <= j) { // when element in the list is less then target sum
32+
t[i][j] = (t[i - 1][j - arr[i - 1]] + t[i - 1][j]) % mod; // either exclude or inxlude and add both of them to get final count
33+
}
34+
else {
35+
t[i][j] = (t[i - 1][j]) % mod; // exclude when element in the list is greater then the sum
36+
}
37+
}
38+
}
39+
return t[n][sum];
40+
}
41+
};

07_Minimum_subset_sum_difference.cpp

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Question Link :- https://www.geeksforgeeks.org/problems/minimum-sum-partition3317/1
2+
// Minimum sum partition
3+
4+
// Note :- 0 < arr[i] <= 10^5
5+
6+
// T.C = O(n*sum)
7+
// S.C = O(n*sum)
8+
class Solution {
9+
public:
10+
vector<int> isSubsetPoss(int arr[], int n, int sum) {
11+
bool t[n + 1][sum + 1];
12+
// initialization
13+
for (int i = 0; i <= n; i++) {
14+
for (int j = 0; j <= sum; j++) {
15+
if (i == 0) {
16+
t[i][j] = false;
17+
}
18+
if (j == 0) {
19+
t[i][j] = true;
20+
}
21+
}
22+
}
23+
for (int i = 1; i <= n; i++) {
24+
for (int j = 1; j <= sum; j++) {
25+
if (arr[i - 1] <= j) {
26+
t[i][j] = t[i - 1][j - arr[i - 1]] || t[i - 1][j]; // include or exclude
27+
} else {
28+
t[i][j] = t[i - 1][j]; // exclude
29+
}
30+
}
31+
}
32+
vector<int> v; // contains all subset sums possible with n elements
33+
for (int j = 0; j <= sum; j++) {
34+
if (t[n][j] == true) {
35+
v.push_back(j); {
36+
}
37+
}
38+
}
39+
return v;
40+
}
41+
42+
int minDifference(int arr[], int n) {
43+
int range = 0;
44+
for(int i=0; i<n; i++) {
45+
range += arr[i];
46+
}
47+
vector<int> v = isSubsetPoss(arr, n, range);
48+
int mn = INT_MAX;
49+
for(int i=0; i< v.size(); i++) {
50+
mn = min(mn, abs(range - 2*v[i]));
51+
}
52+
return mn;
53+
}
54+
};
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Question Link :- https://www.geeksforgeeks.org/problems/partitions-with-given-difference/1
2+
// Partitions with Given Difference
3+
4+
// T.C = O(n*sum(arr))
5+
// S.C = O(sum(arr))
6+
class Solution {
7+
public:
8+
int mod = 1e9+7;
9+
int CountSubsetsWithSum(vector<int>& arr, int n, int sum) {
10+
// int t[n + 1][sum + 1];
11+
vector<vector<int>> t(n + 1, vector<int>(sum + 1, 0));
12+
// initialization
13+
for (int i = 0; i <= n; i++) { // i -> size of the array
14+
for (int j = 0; j <= sum; j++) { // j -> target sum (subset sum)
15+
if (i == 0) {
16+
t[i][j] = 0;
17+
}
18+
if (j == 0) {
19+
t[i][j] = 1;
20+
}
21+
}
22+
}
23+
24+
for (int i = 1; i <= n; i++) {
25+
for (int j = 0; j <= sum; j++) {
26+
if (arr[i - 1] <= j) { // when element in the list is less then target sum
27+
t[i][j] = (t[i - 1][j - arr[i - 1]] + t[i - 1][j]) % mod; // either exclude or inxlude and add both of them to get final count
28+
} else {
29+
t[i][j] = (t[i - 1][j]); // exclude when element in the list is greater then the sum
30+
}
31+
}
32+
}
33+
return t[n][sum]; // finally return the last row and last column element
34+
}
35+
36+
int countPartitions(int n, int diff, vector<int>& arr) {
37+
int sumOfArray = 0;
38+
for (int i = 0; i < n; i++) {
39+
sumOfArray += arr[i];
40+
}
41+
if ((sumOfArray + diff) % 2 != 0) {
42+
return 0;
43+
}
44+
return CountSubsetsWithSum(arr, n, (sumOfArray + diff) / 2);
45+
}
46+
};

09_TargetSum.cpp

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Question Link :- https://leetcode.com/problems/target-sum/
2+
// Target Sum
3+
4+
// T.C = O(n*sum(arr))
5+
// S.C = O(sum(arr))
6+
class Solution {
7+
public:
8+
int CountSubsetsWithSum(vector<int>& arr, int n, int sum) {
9+
int t[n + 1][sum + 1];
10+
for (int i = 0; i <= n; i++) {
11+
for (int j = 0; j <= sum; j++) {
12+
if (i == 0) {
13+
t[i][j] = 0;
14+
}
15+
if (j == 0) {
16+
t[i][j] = 1;
17+
}
18+
}
19+
}
20+
for (int i = 1; i <= n; i++) {
21+
for (int j = 0; j <= sum; j++) {
22+
if (arr[i - 1] <= j) {
23+
t[i][j] = t[i - 1][j - arr[i - 1]] + t[i - 1][j];
24+
} else {
25+
t[i][j] = t[i - 1][j];
26+
}
27+
}
28+
}
29+
return t[n][sum];
30+
}
31+
32+
int findTargetSumWays(vector<int>& nums, int target) {
33+
target = abs(target);
34+
int n = nums.size();
35+
int sum = 0;
36+
for(int i=0; i<n; i++) {
37+
sum += nums[i];
38+
}
39+
if(sum<target || (sum + target)%2 != 0) {
40+
return 0;
41+
}
42+
return CountSubsetsWithSum(nums, n, (sum + target)/2);
43+
}
44+
};

0 commit comments

Comments
 (0)