├── DAY-01 └── 0-1 Knapsack Recursion.cpp ├── DAY-02 ├── 0-1 Knapsack Memoisation.cpp └── 0-1_Knapsack_Problem_(GEEKSFORGEEKS).cpp ├── DAY-03 ├── KNAPSACK-The_Knapsack_Problem(SPOJ).cpp └── Knapsack Problem Bottom-Up.cpp ├── DAY-04 ├── PARTY-Party_Schedule(SPOJ).cpp ├── Partition_Equal_Subset_Sum(LEETCODE).cpp ├── Subset Sum.cpp └── Subset_Sum_Problem(GEEKSFORGEEKS).cpp ├── DAY-05 ├── Count of Subsets With SUM.cpp └── Perfect_Sum_Problem(GEEKSFORGEEKS).cpp ├── DAY-06 ├── Minimum_sum_partition(GEEKSFORGEEKS).cpp └── Subset Sum.cpp ├── DAY-07 ├── Minimum_Difference_Subsets!(INTERVIEWBIT).cpp ├── Target_Sum(LEETCODE).cpp └── count subsets with given difference.cpp ├── DAY-08 ├── Knapsack(HACKERRANK).cpp ├── Knapsack_with_Duplicate_Items(GEEKSFORGEEKS).cpp └── Unbounded Knapsack.cpp ├── DAY-09 ├── Reach_a_given_score(GEEKSFORGEEKS).cpp └── Rod_Cutting(GEEKSFORGEEKS).cpp ├── DAY-10 ├── Coin_Change(GEEKSFORGEEKS).cpp ├── Coin_Change_2(LEETCODE).cpp └── The_Coin_Change_Problem(HACKERRANK).cpp ├── DAY-11 ├── Coin_Change(LEETCODE).cpp ├── Maximize_The_Cut_Segments(GEEKSFORGEEKS).cpp └── Number_of_Coins(GEEKSFORGEEKS).cpp ├── DAY-12 └── Longest Common Subsequence(Recursive).cpp ├── DAY-13 ├── Longest Common Subsequence(Memoisation).cpp └── Longest_Common_Subsequence(GEEKSFORGEEKS).cpp ├── DAY-14 ├── Longest Common Subsequence(Top-Down Approach).cpp └── Longest_Common_Subsequence(LEETCODE).cpp ├── DAY-15 ├── Longest Common Substring.cpp ├── Longest_Common_Substring(GEEKSFORGEEKS).cpp └── Maximum_Length_of_Repeated_Subarray(LEETCODE).cpp ├── DAY-16 ├── Printing Longest Common Subsequence.cpp └── The_Longest_Common_Subsequence(HACKERRANK).cpp ├── DAY-17 ├── Shortest Common Supersequence.cpp └── Shortest_Common_Supersequence(GEEKSFORGEEKS).cpp ├── DAY-18 └── Minimum_number_of_deletions_and_insertions(GEEKSFORGEEKS).cpp ├── DAY-19 ├── Longest_Palindromic_Subsequence(GEEKSFORGEEKS).cpp ├── Longest_Palindromic_Subsequence(INTERVIEWBIT).cpp └── Longest_Palindromic_Subsequence(LEETCODE).cpp ├── DAY-20 └── Minimum_Deletions(GEEKSFORGEEKS).cpp ├── DAY-21 └── Shortest_Common_Supersequence(LEETCODE).cpp ├── DAY-22 ├── Longest_Repeating_Subsequence(GEEKSFORGEEKS).cpp └── Repeating_Sub-Sequence(INTERVIEWBIT).cpp ├── DAY-23 └── Subsequence Matching.cpp ├── DAY-24 ├── Form_a_palindrome(GEEKSFORGEEKS).cpp ├── Minimum_Insertion_Steps_to_Make_a_String_Palindrome(LEETCODE).cpp └── Palindrome_2000(SPOJ).cpp ├── DAY-25 └── Matrix Chain Multiplication(Recursive).cpp ├── DAY-26 └── Matrix Chain Multiplication(Memoisation).cpp └── README.md /DAY-01/0-1 Knapsack Recursion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int knapsack(vector weight, vector value, int maxweight, int index) 5 | { 6 | //base condition 7 | if( (index < 0) || (maxweight <= 0) ) 8 | return 0; 9 | 10 | if(weight[index] <= maxweight) 11 | return max(value[index] + knapsack(weight, value, maxweight - weight[index], index-1), knapsack(weight, value, maxweight, index-1)); 12 | 13 | else 14 | return knapsack(weight, value, maxweight, index-1); 15 | } 16 | 17 | 18 | int main() 19 | { 20 | int n; 21 | cout<<"Enter the number of items: "; 22 | cin>>n; 23 | vector weight(n), value(n); 24 | 25 | cout<<"enter weights: "<>weight[i]; 28 | cout<<"enter values: "<>value[i]; 31 | 32 | int maxweight(0); 33 | 34 | cout<<"Enter maximum capacity of knapsack: "; 35 | cin>>maxweight; 36 | 37 | cout<<"Maximum value of bag is: "< 2 | #define MAX 1005 3 | using namespace std; 4 | 5 | vector < vector > dp(MAX, vector (MAX, -1)); 6 | 7 | int knapsack(vector weight, vector value, int maxweight, int index) 8 | { 9 | //base condition 10 | if( (index < 0) || (maxweight <= 0) ) 11 | return 0; 12 | if(dp[index][maxweight] == -1) 13 | { 14 | if(weight[index] <= maxweight) 15 | dp[index][maxweight] = max(value[index] + knapsack(weight, value, maxweight - weight[index], index-1), knapsack(weight, value, maxweight, index-1)); 16 | 17 | else 18 | dp[index][maxweight] = knapsack(weight, value, maxweight, index-1); 19 | } 20 | return dp[index][maxweight]; 21 | } 22 | 23 | 24 | 25 | int main() 26 | { 27 | int n; 28 | cout<<"Enter the number of items: "; 29 | cin>>n; 30 | vector weight(n), value(n); 31 | 32 | cout<<"enter weights: "<>weight[i]; 35 | cout<<"enter values: "<>value[i]; 38 | 39 | 40 | int maxweight; 41 | 42 | cout<<"Enter maximum capacity of knapsack: "; 43 | cin>>maxweight; 44 | 45 | cout< 2 | #define lli long long int 3 | #define endl "\n" 4 | #define MAX 1005 5 | using namespace std; 6 | 7 | int dp[1002][1002]; 8 | 9 | int knapsack(int weight[], int value[], int w, int index) 10 | { 11 | if((index < 0) || (w == 0)) 12 | return 0; 13 | 14 | if(dp[index][w] != -1) 15 | return dp[index][w]; 16 | 17 | if(weight[index] <= w) 18 | return dp[index][w] = max((value[index] + knapsack(weight, value, w - weight[index], index-1)),knapsack(weight, value, w, index-1)); 19 | else 20 | return dp[index][w] = knapsack(weight, value, w, index-1); 21 | } 22 | 23 | int main() 24 | { 25 | int t; 26 | cin>>t; 27 | while(t--) 28 | { 29 | memset(dp, -1, sizeof(dp)); 30 | int n, w; 31 | cin>>n>>w; 32 | int weight[n], value[n]; 33 | for(int i =0; i>value[i]; 35 | for(int i =0; i>weight[i]; 37 | cout< 2 | #define lli long long int 3 | using namespace std; 4 | 5 | lli dp[2005][2005]; 6 | 7 | lli knapsack(lli weight[], lli value[], lli w, lli index) 8 | { 9 | if((index <0)|| (w <= 0)) 10 | return 0; 11 | 12 | if(dp[index][w] != -1) 13 | return dp[index][w]; 14 | if(weight[index] <= w) 15 | dp[index][w] = max( (value[index] + knapsack(weight, value, w - weight[index], index-1)), knapsack(weight, value, w, index-1)); 16 | else 17 | dp[index][w] = knapsack(weight, value, w, index-1); 18 | 19 | return dp[index][w]; 20 | } 21 | 22 | 23 | 24 | int main() 25 | { 26 | 27 | lli n,w; 28 | cin>>w>>n; 29 | lli weight[n], value[n]; 30 | 31 | for(lli i =0; i>weight[i]>>value[i]; 33 | 34 | memset(dp, -1, sizeof(dp)); 35 | 36 | cout< 2 | #define lli long long int 3 | #define endl "\n" 4 | #define MAX 1005 5 | using namespace std; 6 | 7 | 8 | int knapsack(int weight[], int value[], int w, int n) 9 | { 10 | int dp[n+1][w+1]; 11 | 12 | //initialisation condition 13 | for(int i=0;i>t; 35 | while(t--) 36 | { 37 | int n, w; 38 | cin>>n>>w; 39 | int weight[n], value[n]; 40 | for(int i =0; i>value[i]; 42 | for(int i =0; i>weight[i]; 44 | cout< 2 | #define lli long long int 3 | #define endl "\n" 4 | #define MAX 1000005 5 | #define MOD 1000000007 6 | using namespace std; 7 | 8 | void knapsack(int cost[], int fun[], int p, int n) 9 | { 10 | int dp[n+1][p+1]; 11 | for(int i=0; i j) 36 | idx = j; 37 | } 38 | } 39 | } 40 | cout<>p>>n; 49 | if((p == 0) && (n==0)) 50 | break; 51 | int cost[n], fun[n]; 52 | for(int i =0; i>cost[i]>>fun[i]; 54 | knapsack(cost, fun, p, n); 55 | } 56 | return 0; 57 | } -------------------------------------------------------------------------------- /DAY-04/Partition_Equal_Subset_Sum(LEETCODE).cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool canPartition(vector& nums) { 4 | int sum = accumulate(nums.begin(), nums.end(), 0); 5 | if(sum%2) 6 | return false; 7 | sum /= 2; 8 | int n =nums.size(); 9 | bool dp[n+1][sum+1]; 10 | 11 | for(int i =0; i 2 | #define lli long long int 3 | #define endl "\n" 4 | using namespace std; 5 | 6 | /************* Bottom Up Approach********************/ 7 | int subset(int arr[], int n, int sum) 8 | { 9 | // code here 10 | 11 | bool dp[n+1][sum+1]; 12 | for(int i =0; i 2 | #define lli long long int 3 | #define endl "\n" 4 | using namespace std; 5 | 6 | /************* Bottom Up Approach********************/ 7 | int countsubset(int arr[], int n, int sum) 8 | { 9 | int dp[n+1][sum+1]; 10 | for(int i =0; i 2 | using namespace std; 3 | 4 | class Solution{ 5 | 6 | public: 7 | int minDiffernce(int arr[], int n) 8 | { 9 | 10 | int sum = accumulate(arr, arr+n, 0); 11 | 12 | bool dp[n+1][sum+1]; 13 | for(int i = 0; i > t; 41 | while (t--) 42 | { 43 | int n; 44 | cin >> n; 45 | 46 | int a[n]; 47 | for(int i = 0; i < n; i++) 48 | cin >> a[i]; 49 | 50 | 51 | 52 | Solution ob; 53 | cout << ob.minDiffernce(a, n) << "\n"; 54 | 55 | } 56 | return 0; 57 | -------------------------------------------------------------------------------- /DAY-06/Subset Sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define lli long long int 3 | #define endl "\n" 4 | using namespace std; 5 | 6 | 7 | int minSumDiffernce(int arr[], int n) 8 | { 9 | 10 | int sum = accumulate(arr, arr+n, 0); 11 | 12 | //declaring dp 13 | bool dp[n+1][sum+1]; 14 | 15 | //initialisation step 16 | for(int i = 0; i 2 | int Solution::solve(vector &arr) { 3 | int sum = accumulate(arr.begin(), arr.end(), 0); 4 | int n = arr.size(); 5 | bool dp[n+1][sum+1]; 6 | for(int i = 0; i & arr, int S) { 4 | 5 | int range(0), cz(0); 6 | for(auto c: arr) 7 | if(c == 0) 8 | cz++; 9 | else 10 | range +=c; 11 | 12 | if(S > range) 13 | return 0; 14 | 15 | int reqsum = (S + range); 16 | 17 | if((reqsum % 2) == 1) 18 | return 0; 19 | 20 | reqsum /= 2; 21 | 22 | 23 | int n = arr.size(); 24 | int dp[n+1][reqsum+1]; 25 | for(int i = 0; i 2 | #define lli long long int 3 | #define endl "\n" 4 | using namespace std; 5 | 6 | /************* Bottom Up Approach********************/ 7 | int countsubsetwithDiff(int arr[], int n, int dif) 8 | { 9 | int range = accumulate(arr, arr+n,0); 10 | 11 | int reqsum = (dif + range); 12 | 13 | if((reqsum % 2) == 1) 14 | return 0; 15 | reqsum /= 2; 16 | int dp[n+1][reqsum+1]; 17 | for(int i = 0; i 2 | using namespace std; 3 | 4 | int uk(int arr[], int n, int k) 5 | { 6 | int dp[n+1][k+1]; 7 | for(int i=0; i>t; 28 | while(t--) 29 | { 30 | int n, k; 31 | cin>>n>>k; 32 | int arr[n]; 33 | for(int i =0; i>arr[i]; 35 | cout< 2 | #define lli long long int 3 | #define endl "\n" 4 | #define MAX 1005 5 | using namespace std; 6 | 7 | 8 | int unbounded_knapsack(int weight[], int value[], int w, int n) 9 | { 10 | int dp[n+1][w+1]; 11 | 12 | //it is similar to the knapsack problem but just we need to call for same item when we chose that particluar item 13 | //initialisation condition 14 | for(int i=0;i>t; 36 | while(t--) 37 | { 38 | int n, w; 39 | cin>>n>>w; 40 | int weight[n], value[n]; 41 | for(int i =0; i>value[i]; 43 | for(int i =0; i>weight[i]; 45 | cout< 2 | #define lli long long int 3 | #define endl "\n" 4 | using namespace std; 5 | 6 | int knapsack(int sum) 7 | { 8 | int n =3; 9 | int arr[n] = {3,5,10}; 10 | int dp[n+1][sum+1]; 11 | for(int i =0; i>t; 31 | while(t--) 32 | { 33 | int sum; 34 | cin>>sum; 35 | cout< 2 | #define lli long long int 3 | #define endl "\n" 4 | using namespace std; 5 | 6 | int knapsack(int arr[], int n) 7 | { 8 | int dp[n+1][n+1]; 9 | for(int i=0; i>t; 30 | while(t--) 31 | { 32 | int n; 33 | cin>>n; 34 | int arr[n]; 35 | for(int i =0; i>arr[i]; 37 | cout<& S) { 4 | if(amount == 0) 5 | return 1; 6 | int m =S.size(); 7 | int n = amount; 8 | int dp[m+1][n+1]; 9 | for(int i=0;i<=m;i++) 10 | dp[i][0]=1; 11 | 12 | for(int i=1;i<=n;i++) 13 | dp[0][i]=0; 14 | 15 | for(int i=1;i<=m;i++) 16 | { 17 | for(int j=1;j<=n;j++) 18 | { 19 | if(S[i-1]<=j) 20 | { 21 | dp[i][j]= dp[i][j-S[i-1]] + dp[i-1][j]; 22 | } 23 | else 24 | { 25 | dp[i][j]=dp[i-1][j]; 26 | } 27 | } 28 | } 29 | 30 | if(dp[m][n] == 0) 31 | return 0; 32 | return dp[m][n]; 33 | } 34 | 35 | }; -------------------------------------------------------------------------------- /DAY-10/The_Coin_Change_Problem(HACKERRANK).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | string ltrim(const string &); 6 | string rtrim(const string &); 7 | vector split(const string &); 8 | 9 | /* 10 | * Complete the 'getWays' function below. 11 | * 12 | * The function is expected to return a LONG_INTEGER. 13 | * The function accepts following parameters: 14 | * 1. INTEGER n 15 | * 2. LONG_INTEGER_ARRAY c 16 | */ 17 | 18 | long getWays(int n, vector S) { 19 | int m = S.size(); 20 | long dp[m+1][n+1]; 21 | for(int i=0;i<=m;i++) 22 | dp[i][0]=1; 23 | 24 | for(int i=1;i<=n;i++) 25 | dp[0][i]=0; 26 | 27 | for(int i=1;i<=m;i++) 28 | { 29 | for(int j=1;j<=n;j++) 30 | { 31 | if(S[i-1]<=j) 32 | { 33 | dp[i][j]= dp[i][j-S[i-1]] + dp[i-1][j]; 34 | } 35 | else 36 | { 37 | dp[i][j]=dp[i-1][j]; 38 | } 39 | } 40 | } 41 | 42 | return dp[m][n]; 43 | } 44 | 45 | int main() 46 | { 47 | ofstream fout(getenv("OUTPUT_PATH")); 48 | 49 | string first_multiple_input_temp; 50 | getline(cin, first_multiple_input_temp); 51 | 52 | vector first_multiple_input = split(rtrim(first_multiple_input_temp)); 53 | 54 | int n = stoi(first_multiple_input[0]); 55 | 56 | int m = stoi(first_multiple_input[1]); 57 | 58 | string c_temp_temp; 59 | getline(cin, c_temp_temp); 60 | 61 | vector c_temp = split(rtrim(c_temp_temp)); 62 | 63 | vector c(m); 64 | 65 | for (int i = 0; i < m; i++) { 66 | long c_item = stol(c_temp[i]); 67 | 68 | c[i] = c_item; 69 | } 70 | 71 | // Print the number of ways of making change for 'n' units using coins having the values given by 'c' 72 | 73 | long ways = getWays(n, c); 74 | 75 | fout << ways << "\n"; 76 | 77 | fout.close(); 78 | 79 | return 0; 80 | } 81 | 82 | string ltrim(const string &str) { 83 | string s(str); 84 | 85 | s.erase( 86 | s.begin(), 87 | find_if(s.begin(), s.end(), not1(ptr_fun(isspace))) 88 | ); 89 | 90 | return s; 91 | } 92 | 93 | string rtrim(const string &str) { 94 | string s(str); 95 | 96 | s.erase( 97 | find_if(s.rbegin(), s.rend(), not1(ptr_fun(isspace))).base(), 98 | s.end() 99 | ); 100 | 101 | return s; 102 | } 103 | 104 | vector split(const string &str) { 105 | vector tokens; 106 | 107 | string::size_type start = 0; 108 | string::size_type end = 0; 109 | 110 | while ((end = str.find(" ", start)) != string::npos) { 111 | tokens.push_back(str.substr(start, end - start)); 112 | 113 | start = end + 1; 114 | } 115 | 116 | tokens.push_back(str.substr(start)); 117 | 118 | return tokens; 119 | } -------------------------------------------------------------------------------- /DAY-11/Coin_Change(LEETCODE).cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int coinChange(vector& coins, int amount) { 4 | int n = coins.size(); 5 | int dp[n+1][amount+1]; 6 | for(int i =0; i 2 | 3 | using namespace std; 4 | int check(int val[],int n,int w) 5 | { 6 | int dp[n+1][w+1]; 7 | for(int i= 0; i>t; 36 | while(t--) 37 | { 38 | int W; 39 | cin>>W; 40 | int val[3]; 41 | for(int i=0;i<3;i++) 42 | { 43 | cin>>val[i]; 44 | } 45 | cout< 2 | #define lli long long int 3 | #define endl "\n" 4 | #define MAX 1000005 5 | #define MOD 1000000007 6 | using namespace std; 7 | 8 | int LCS(string s1, string s2, int n, int m){ 9 | if((n==0) || (m==0)) return 0; 10 | if(s1[n-1] == s2[m-1]) return 1 + LCS(s1,s2,n-1,m-1); 11 | return max(LCS(s1,s2,n, m-1), LCS(s1,s2,n-1,m)); 12 | } 13 | 14 | int main() 15 | { 16 | string s1, s2; 17 | cin>>s1>>s2; 18 | cout< 2 | #define lli long long int 3 | #define endl "\n" 4 | #define MAX 10005 5 | #define MOD 1000000007 6 | using namespace std; 7 | 8 | int dp[MAX][MAX]; 9 | int LCS(string s1, string s2, int n, int m){ 10 | 11 | if((n==0) || (m==0)) return 0; 12 | 13 | if(dp[n][m] == -1) 14 | { 15 | if(s1[n-1] == s2[m-1]) 16 | dp[n][m] = 1 + LCS(s1,s2,n-1,m-1); 17 | else 18 | dp[n][m] = max(LCS(s1,s2,n, m-1), LCS(s1,s2,n-1,m)); 19 | } 20 | 21 | return dp[n][m]; 22 | } 23 | 24 | int main() 25 | { 26 | memset(dp, -1, sizeof(dp)); 27 | string s1 = "ABCDE"; 28 | string s2 = "AKLDFGE"; 29 | cout< 2 | #define lli long long int 3 | #define endl "\n" 4 | #define MAX 105 5 | #define MOD 1000000007 6 | using namespace std; 7 | 8 | int dp[MAX][MAX]; 9 | int LCS(string s1, string s2, int n, int m){ 10 | 11 | if((n==0) || (m==0)) return 0; 12 | 13 | if(dp[n][m] == -1) 14 | { 15 | if(s1[n-1] == s2[m-1]) 16 | dp[n][m] = 1 + LCS(s1,s2,n-1,m-1); 17 | else 18 | dp[n][m] = max(LCS(s1,s2,n, m-1), LCS(s1,s2,n-1,m)); 19 | } 20 | 21 | return dp[n][m]; 22 | } 23 | 24 | int main() 25 | { 26 | int t; 27 | cin>>t; 28 | while(t--) 29 | { 30 | memset(dp, -1, sizeof(dp)); 31 | int n1, n2; 32 | cin>>n1>>n2; 33 | string s1, s2; 34 | cin>>s1>>s2; 35 | cout< 2 | #define lli long long int 3 | #define endl "\n" 4 | #define MAX 10005 5 | #define MOD 1000000007 6 | using namespace std; 7 | 8 | int dp[MAX][MAX]; 9 | int LCS(string s1, string s2, int n, int m){ 10 | 11 | int dp[n+1][m+1]; 12 | 13 | for(int i =0; i 2 | #define lli long long int 3 | #define endl "\n" 4 | #define MAX 105 5 | #define MOD 1000000007 6 | using namespace std; 7 | 8 | int LCS(string s1, string s2, int n, int m){ 9 | 10 | int dp[n+1][m+1]; 11 | 12 | for(int i =0; i 2 | #define lli long long int 3 | #define endl "\n" 4 | #define MAX 105 5 | #define MOD 1000000007 6 | using namespace std; 7 | 8 | int LCS(string s1, string s2, int n, int m){ 9 | 10 | int dp[n+1][m+1]; 11 | 12 | for(int i =0; i>t; 39 | while(t--) 40 | { 41 | int n1, n2; 42 | cin>>n1>>n2; 43 | string s1, s2; 44 | cin>>s1>>s2; 45 | cout< 2 | #define lli long long int 3 | #define endl "\n" 4 | #define MAX 10005 5 | #define MOD 1000000007 6 | using namespace std; 7 | 8 | string LCS(string s1, string s2, int n, int m){ 9 | 10 | int dp[n+1][m+1]; 11 | 12 | for(int i =0; i0) && (j > 0)) 31 | { 32 | if(s1[i-1] == s2[j-1]) 33 | { 34 | answer = s1[i-1] + answer; 35 | i--, j--; 36 | } 37 | else 38 | { 39 | if(dp[i][j-1] == dp[i][j]) 40 | j--; 41 | else 42 | i--; 43 | } 44 | } 45 | 46 | 47 | return answer; 48 | } 49 | 50 | int main() 51 | { 52 | string s1 = "ABCDE"; 53 | string s2 = "AKLDFGE"; 54 | cout< 2 | #define lli long long int 3 | #define endl "\n" 4 | #define MAX 10005 5 | #define MOD 1000000007 6 | using namespace std; 7 | 8 | vector LCS(vector s1, vector s2, int n, int m){ 9 | 10 | int dp[n+1][m+1]; 11 | 12 | for(int i =0; i answer; 30 | while((i >0) && (j > 0)) 31 | { 32 | if(s1[i-1] == s2[j-1]) 33 | { 34 | answer.insert(answer.begin(),s1[i-1]); 35 | i--, j--; 36 | } 37 | else 38 | { 39 | if(dp[i][j-1] == dp[i][j]) 40 | j--; 41 | else 42 | i--; 43 | } 44 | } 45 | 46 | 47 | return answer; 48 | } 49 | 50 | int main() 51 | { 52 | int n1, n2; 53 | cin>>n1>>n2; 54 | vector v1(n1), v2(n2); 55 | for(int i=0;i>v1[i]; 57 | for(int i=0;i>v2[i]; 59 | vector v3 = LCS(v1, v2, n1, n2); 60 | for(auto c: v3) 61 | cout< 2 | #define lli long long int 3 | #define endl "\n" 4 | #define MAX 10005 5 | #define MOD 1000000007 6 | using namespace std; 7 | 8 | int dp[MAX][MAX]; 9 | int LCS(string s1, string s2, int n, int m){ 10 | 11 | int dp[n+1][m+1]; 12 | 13 | for(int i =0; i 2 | #define lli long long int 3 | #define endl "\n" 4 | using namespace std; 5 | 6 | int SCS(string s1, string s2, int n, int m) 7 | { 8 | int dp[n+1][m+1]; 9 | for(int i=0; i>t; 30 | while(t--) 31 | { 32 | string s1, s2; 33 | cin>>s1>>s2; 34 | cout< 2 | using namespace std; 3 | 4 | 5 | class Solution{ 6 | 7 | 8 | public: 9 | int minOperations(string s1, string s2) 10 | { 11 | // Your code goes here 12 | int n = s1.length(); 13 | int m = s2.length(); 14 | int dp[n+1][m+1]; 15 | for(int i=0; i> t; 40 | while (t--) 41 | { 42 | string s1, s2; 43 | cin >> s1 >> s2; 44 | 45 | Solution ob; 46 | cout << ob.minOperations(s1, s2) << "\n"; 47 | 48 | } 49 | return 0; 50 | } -------------------------------------------------------------------------------- /DAY-19/Longest_Palindromic_Subsequence(GEEKSFORGEEKS).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define lli long long int 3 | #define endl "\n" 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | lli t; 9 | cin>>t; 10 | while(t--) 11 | { 12 | string s1; 13 | cin>>s1; 14 | 15 | string s2(s1); 16 | reverse(s2.begin(), s2.end()); 17 | int n = s1.length(); 18 | int dp[n+1][n+1]; 19 | for(int i=0; i 2 | #define lli long long int 3 | #define endl "\n" 4 | using namespace std; 5 | 6 | int MinDel(string s1) 7 | { 8 | int n = s1.length(); 9 | string s2(s1); 10 | reverse(s2.begin(), s2.end()); 11 | int dp[n+1][n+1]; 12 | for(int i=0; i>t; 31 | while(t--) 32 | { 33 | string s; 34 | cin>>s; 35 | cout<0) && (j > 0)) 26 | { 27 | if(s1[i-1] == s2[j-1]) 28 | { 29 | answer = s1[i-1] + answer; 30 | i--, j--; 31 | } 32 | else 33 | { 34 | if(dp[i][j-1] == dp[i][j]){ 35 | answer = s2[j-1] + answer; 36 | j--; 37 | } 38 | 39 | else{ 40 | answer = s1[i-1] + answer; 41 | i--; 42 | } 43 | 44 | } 45 | } 46 | while(i > 0) 47 | { 48 | answer = s1[i-1] + answer; 49 | i--; 50 | } 51 | while(j > 0) 52 | { 53 | answer = s2[j-1] + answer; 54 | j--; 55 | } 56 | 57 | 58 | return answer; 59 | } 60 | 61 | 62 | 63 | }; -------------------------------------------------------------------------------- /DAY-22/Longest_Repeating_Subsequence(GEEKSFORGEEKS).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define lli long long int 3 | #define endl "\n" 4 | #define MAX 105 5 | #define MOD 1000000007 6 | using namespace std; 7 | 8 | 9 | int LRS(string s1, string s2, int n, int m){ 10 | 11 | int dp[n+1][m+1]; 12 | 13 | for(int i =0; i>t; 35 | while(t--) 36 | { 37 | int n; 38 | cin>>n; 39 | string s; 40 | cin>>s; 41 | cout< 2 | #define lli long long int 3 | #define endl "\n" 4 | #define MAX 10005 5 | #define MOD 1000000007 6 | using namespace std; 7 | 8 | int dp[MAX][MAX]; 9 | bool LCS(string s1, string s2, int n, int m){ 10 | 11 | int dp[n+1][m+1]; 12 | 13 | for(int i =0; i 2 | #define lli long long int 3 | #define endl "\n" 4 | using namespace std; 5 | 6 | int MIP(string s1) 7 | { 8 | string s2(s1); 9 | reverse(s2.begin(), s2.end()); 10 | int n = s1.length(); 11 | int dp[n+1][n+1]; 12 | for(int i=0; i>t; 30 | while(t--) 31 | { 32 | string s; 33 | cin>>s; 34 | cout< 2 | #define lli long long int 3 | #define endl "\n" 4 | using namespace std; 5 | 6 | lli MIP(string s1) 7 | { 8 | string s2(s1); 9 | reverse(s2.begin(), s2.end()); 10 | lli n = s1.length(); 11 | lli dp[n+1][n+1]; 12 | for(lli i=0; i>t; 30 | string s; 31 | cin>>s; 32 | cout< 2 | #define lli long long int 3 | #define endl "\n" 4 | #define MAX 1000005 5 | #define MOD 1000000007 6 | using namespace std; 7 | 8 | int MCM(int arr[], int n, int i, int j) 9 | { 10 | if(i >= j) 11 | return 0; 12 | int mini = INT_MAX; 13 | for(int k = i; k < j; k++) 14 | { 15 | mini = min(mini, MCM(arr,n,i,k) + MCM(arr,n,k+1,j) + (arr[i-1]*arr[k]*arr[j])); 16 | } 17 | return mini; 18 | } 19 | 20 | int main() 21 | { 22 | int arr[5] = {40, 20, 30, 10, 30}; 23 | cout< 2 | #define lli long long int 3 | #define endl "\n" 4 | #define MAX 10005 5 | #define MOD 1000000007 6 | using namespace std; 7 | 8 | int dp[MAX][MAX]; 9 | int MCM(int arr[], int n, int i, int j) 10 | { 11 | if(i >= j) 12 | return 0; 13 | if(dp[i][j] == -1) 14 | { 15 | int mini = INT_MAX; 16 | for(int k = i; k < j; k++) 17 | { 18 | mini = min(mini, MCM(arr,n,i,k) + MCM(arr,n,k+1,j) + (arr[i-1]*arr[k]*arr[j])); 19 | } 20 | dp[i][j] = mini; 21 | } 22 | 23 | return dp[i][j]; 24 | } 25 | 26 | int main() 27 | { 28 | memset(dp, -1, sizeof(dp)); 29 | int arr[5] = {40, 20, 30, 10, 30}; 30 | cout< 5 | 6 | 7 | ### Definition : :orange_book: 8 | ``` 9 | Simply cached Recursion (Memoization) or in other words enhanced recursion. 10 | ``` 11 | ##### _The main idea behind DP is to store the results of subproblems so that we can simply use the results of these subproblems without re-computing them when needed in the future. This idealogy saves a lot of time and makes the solution optimized._ 12 | --- 13 | ## Day - 1 :blue_book: 14 | ### Knapsack Problem: :pushpin::pushpin: 15 | _In this problem we are given an empty bag with its maximum weight holding capacity. Also, we are given a list of items with there weight and profit values. We need to find out the maximum profit we can earn._ 16 | #### Type of Knapsack Problems 17 | - ***Fractional Knapsack*** - It is simply a greedy problem. In this, we can take fraction values. ***for eg. if we have space of 3 kg. left in a knapsack and we have an item of 6 kg that values 50 rs. , then we can take 50% of that item and we will gain a profit of 25 rs.*** 18 | 19 | - ***0/1 Knapsack*** - It is a classical DP problem. Many beginner-level problems are a variation of this problem. In this, we have only had two choices, either we include the item in Knapsack or we don't. 20 | 21 | - ***Unbounded Knapsack*** - It is similar to 0/1 Knapsack but in this, we can include the same item multiple numbers of times. 22 | 23 | 24 | ```diff 25 | ! solved a classical knapsack problem using only recursion. 26 | ``` 27 | ##### 🎥Tutorial Link - https://www.youtube.com/watch?v=l02UxPYRmCQ&list=PL_z_8CaSLPWekqhdCPmFohncHwz8TY2Go&index=2 28 | 29 | #### It all started with Day-1 :fire::fire: 30 | --- 31 | 32 | ## Day - 2 :green_book: 33 | ### Memoization: Top - Down Approach :pushpin::pushpin: 34 | _It is an **optimization technique** used to cache the results of subproblems so that we can use that results later on if required. **Memoization** ensures that a method doesn't run for the inputs whose results are previously calculated._ 35 | 36 | The dimensions of the memoization array/table depend upon variables. 37 | If in recursive function: 38 | 39 | - 1 variable is changing on recursive call, we will create a linear vector/array. ***E.g. Fibonacci series*** 40 | - 2 variables are changing on recursive call, then we will use a 2-D matrix to store the results of subproblems. ***E.g. Longest Common Subsequence*** 41 | - and so on.....for 3, 4..n variables. 42 | 43 | #### _Generally we should initialize these matrices with -1 and later on we can check that if the value of a particular cell is not -1 then we will directly return that particular cell value. else we will do a recursive call and set the cell value and finally return the cell value._ 44 | 45 | ```diff 46 | + solved a classical knapsack problem using the memoization technique. 47 | ``` 48 | ### Problem solved 49 | | Platform | Title | Solution | Difficulty | 50 | |--------------|---------------- | --------------- |---------------| 51 | GEEKSFORGEEKS |[0 - 1 Knapsack Problem](https://practice.geeksforgeeks.org/problems/0-1-knapsack-problem/0) | [View Solution](./DAY-02/0-1_Knapsack_Problem_(GEEKSFORGEEKS).cpp) | Easy ||| 52 |
 53 | Important Tip -  std::vector's at() function is similar to subscript operator [ ].
 54 | But when the performance is measured at() function is 3.1 times faster then subscript operator [ ]. 
 55 | 
56 | 57 | ##### 🎥Tutorial Link - https://www.youtube.com/watch?v=kvyShbFVaY8&list=PL_z_8CaSLPWekqhdCPmFohncHwz8TY2Go&index=3 58 | ##### 🎥Tutorial Link - https://www.youtube.com/watch?v=fJbIuhs24zQ&list=PL_z_8CaSLPWekqhdCPmFohncHwz8TY2Go&index=4 59 | 60 | This costed me 29 submissions :persevere: :laughing: 61 | 62 | --- 63 | ## Day - 3 :ledger: 64 | ### 1. Tabulation: Bottom-Up Approach :pushpin::pushpin: 65 | _It is one of the most preferable methods in dynamic programming. It is faster than the **memoization** method as it doesn't involve any recursive calls. In this method, we have an array/matrix and we start from the first cell and move down filling entries in each cell one by one._ 66 | #### 2-Steps to create dp matrix. 67 | - ***Step-1 Initialisation*** - It is similar to the base condition which we do in a recursive function. ***for eg.*** 68 | ``` 69 | //in recursive function 70 | if((index <0)|| (w <= 0)) 71 | return 0; 72 | 73 | //in bottom-up approach 74 | for(int i =0; i<=n; i++){ 75 | for(int j= 0; j<=w; j++){ 76 | if(i == 0 || j ==0) 77 | dp[i][j] = 0; 78 | } 79 | } 80 | ``` 81 | 82 | - ***Step-2 Iterative Function*** - We create an iterative function that is similar to the recursive call function. All the conditions will be the same in both the methods, the only difference is that in memoization we do recursive calls whereas in the bottom-up approach we look up for previous cells in the matrix, this makes ***bottom-up approach a faster approach.*** 83 | 84 | ```diff 85 | - solved the classical knapsack problem using bottom-up approach. 86 | ``` 87 | ### Problem solved 88 | | Platform | Title | Solution | Difficulty | 89 | |--------------|---------------- | --------------- |---------------| 90 | SPOJ |[KNAPSACK - The Knapsack Problem](https://www.spoj.com/problems/KNAPSACK/) | [View Solution](./DAY-03/KNAPSACK-The_Knapsack_Problem(SPOJ).cpp) | Easy ||| 91 |
 92 | Important Tip -  The bottom-up approach is preferred over memoization because in the memoization technique 
 93 | we might get stack overflow on doing various recursive calls for large data.
 94 | 
95 | 96 | ##### 🎥Tutorial Link - https://www.youtube.com/watch?v=ntCGbPMeqgg&list=PL_z_8CaSLPWekqhdCPmFohncHwz8TY2Go&index=5 97 | 98 | 99 | #### Though it's a rare condition. :sweat_smile::sweat_smile: 100 | --- 101 | ## Day - 4 :closed_book: 102 | ### Solved: Subset Sum Problem :pushpin::pushpin: 103 | _It is a variation of the knapsack problem in which we are given an array of non-negative integers and a **required sum**. We need to find out that is it possible to create a subset of that array so that sum of elements of the subset equals the **required sum**._ 104 | 105 | ```diff 106 | @@ solved the subset sum problem @@ 107 | ``` 108 | ### Problem solved 109 | | Platform | Title | Solution | Difficulty | 110 | |--------------|---------------- | --------------- |---------------| 111 | SPOJ |[PARTY - Party Schedule](https://www.spoj.com/problems/PARTY/) | [View Solution](./DAY-04/PARTY-Party_Schedule(SPOJ).cpp) | Easy ||| 112 | GEEKSFORGEEKS |[Subset Sum Problem](https://practice.geeksforgeeks.org/problems/subset-sum-problem2014/1) | [View Solution](./DAY-04/Subset_Sum_Problem(GEEKSFORGEEKS).cpp) | Medium ||| 113 | LEETCODE |[Partition Equal Subset Sum](https://leetcode.com/problems/partition-equal-subset-sum/) | [View Solution](./DAY-04/Partition_Equal_Subset_Sum(LEETCODE).cpp) | Medium ||| 114 | 115 | ##### 🎥Tutorial Link - https://www.youtube.com/watch?v=_gPcYovP7wc&list=PL_z_8CaSLPWekqhdCPmFohncHwz8TY2Go&index=7 116 | ##### 🎥Tutorial Link - https://www.youtube.com/watch?v=_gPcYovP7wc&list=PL_z_8CaSLPWekqhdCPmFohncHwz8TY2Go&index=8 117 | 118 | #### Finally DP started showing it's colors. :yellow_heart::blue_heart::purple_heart::green_heart::heart: 119 | 120 | --- 121 | ## Day - 5 :notebook: 122 | ### Solved: Count of Subsets with Required Sum :pushpin::pushpin: 123 | _It is a slight variation of the *Subset Sum Problem* in which we are given an array of non-negative integers and a **required sum**. We need to find out the count of the total number of subsets of the given array whose sum equals the **required sum**._ 124 | 125 | ```diff 126 | ! solved the Count of Subsets with Required Sum Problem 127 | ``` 128 | ### Problem solved 129 | | Platform | Title | Solution | Difficulty | 130 | |--------------|---------------- | --------------- |---------------| 131 | GEEKSFORGEEKS |[Perfect Sum Problem](https://practice.geeksforgeeks.org/problems/perfect-sum-problem5633/1) | [View Solution](./DAY-05/Perfect_Sum_Problem(GEEKSFORGEEKS).cpp) | Medium ||| 132 | 133 | ##### 🎥Tutorial Link - https://www.youtube.com/watch?v=_gPcYovP7wc&list=PL_z_8CaSLPWekqhdCPmFohncHwz8TY2Go&index=9 134 | #### :walking: “I walk slowly, but never backwards.” :walking: - — Abraham Lincoln :pray: :pray: 135 | 136 | --- 137 | ## Day - 6 :orange_book: 138 | ### Solved: Minimum Sum Partition Problem :pushpin::pushpin: 139 | _Again its a variation of the **Subset Sum Problem**. The problem statement states that we are given an array with non-negative integers and our task is to divide the elements of that array into two subsets such that the absolute difference between their sums is **minimum.**_ 140 | 141 | ```diff 142 | + solved the subset sum problem 143 | ``` 144 | ### Problem solved 145 | | Platform | Title | Solution | Difficulty | 146 | |--------------|---------------- | --------------- |---------------| 147 | GEEKSFORGEEKS |[Minimum sum partition](https://practice.geeksforgeeks.org/problems/minimum-sum-partition3317/1) | [View Solution](./DAY-06/Minimum_sum_partition(GEEKSFORGEEKS).cpp) | Hard ||| 148 | ##### 🎥Tutorial Link - https://www.youtube.com/watch?v=_gPcYovP7wc&list=PL_z_8CaSLPWekqhdCPmFohncHwz8TY2Go&index=10 149 | #### Some days are harder than others :confounded: :triangular_flag_on_post: :relieved: 150 | 151 | 152 | --- 153 | ## Day - 7 :blue_book: 154 | ### Solved: Count of Subsets with required difference :pushpin::pushpin: 155 | _This is a variation of **Minimum Sum Partition Problem interesting problem**. In this, we are given an array with non-negative integers and our task is to divide the elements of that array into two subsets such that the absolute difference between their sums equals **required difference.**_ 156 | 157 | ##### Two equations to solve the problem are: 158 | ```cpp 159 | s1 = sum of 1st subset 160 | s2 = sum of 2nd subset 161 | S = required difference 162 | range = sum of entire array 163 | 164 | 165 | s1 - s2 = S //equation 1 166 | s1 + s2 = range //equation 2 167 | 168 | // on solving eq1 and eq2 we get 169 | s1 = (range + S)/2 170 | ``` 171 | ##### Target sum - It's an awesome problem. Must try:- https://leetcode.com/problems/target-sum/ 172 | 173 | ### Problem solved 174 | | Platform | Title | Solution | Difficulty | 175 | |--------------|---------------- | --------------- |---------------| 176 | INTERVIEW BIT |[Minimum Difference Subsets!](https://www.interviewbit.com/problems/minimum-difference-subsets/) | [View Solution](./DAY-07/Minimum_Difference_Subsets!(INTERVIEWBIT).cpp) | Hard ||| 177 | LEETCODE |[Target Sum :rocket:](https://leetcode.com/problems/target-sum/) | [View Solution](./DAY-07/Target_Sum(LEETCODE).cpp) | Hard ||| 178 | 179 | ```diff 180 | - solved count of subsets with given difference 181 | ``` 182 | ##### 🎥Tutorial Link - https://www.youtube.com/watch?v=_gPcYovP7wc&list=PL_z_8CaSLPWekqhdCPmFohncHwz8TY2Go&index=11 183 | ##### 🎥Tutorial Link - https://www.youtube.com/watch?v=_gPcYovP7wc&list=PL_z_8CaSLPWekqhdCPmFohncHwz8TY2Go&index=12 184 | #### :yellow_heart: 7-Days Streak. :blue_heart: 7-Days of CP. :purple_heart: 7-Days of DP :green_heart: 7-Days of OP :heart: 185 | --- 186 | ## Day - 8 :green_book: 187 | ### Solved: Unbounded Knapsack:pushpin::pushpin: 188 | _**Unbounded Knapsack Problems** are slightly different from **0/1 Knapsack Problems**. In this, we can include the same item multiple numbers of times inside the knapsack and our aim is to just maximize the profit._ 189 | ##### We just need to change one condition: 190 | 191 | ```cpp 192 | //0-1 Knapsack 193 | if(weight[i-1] <= j) 194 | dp[i][j] = max((value[i-1] + dp[i-1][j-weight[i-1]]), dp[i-1][j]); 195 | 196 | // Unbounded Knapsack 197 | if(weight[i-1] <= j) 198 | dp[i][j] = max((value[i-1] + dp[i][j-weight[i-1]]), dp[i-1][j]); 199 | ``` 200 | 201 | ### Problem solved 202 | | Platform | Title | Solution | Difficulty | 203 | |--------------|---------------- | --------------- |---------------| 204 | HACKERRANK |[Knapsack](https://www.hackerrank.com/challenges/unbounded-knapsack/problem) | [View Solution](./DAY-08/Knapsack(HACKERRANK).cpp) | Medium ||| 205 | GEEKSFORGEEKS |[Knapsack with Duplicate Items](https://practice.geeksforgeeks.org/problems/knapsack-with-duplicate-items4201/1) | [View Solution](./DAY-08/Knapsack_with_Duplicate_Items(GEEKSFORGEEKS).cpp) | Medium ||| 206 | ```diff 207 | @@ solved Unbounded Knapsack Problem @@ 208 | ``` 209 | 210 | ##### 🎥Tutorial Link - https://www.youtube.com/watch?v=_gPcYovP7wc&list=PL_z_8CaSLPWekqhdCPmFohncHwz8TY2Go&index=13 211 | 212 | #### Visited HackerRank after so Long ! Nostalgic :dizzy: :smile: 213 | --- 214 | 215 | ## Day - 9 :ledger: 216 | ### Solved: Rod Cutting Problem:pushpin::pushpin: 217 | _**Problem Statement:** Given a rod of length **n** and an array of prices that contains prices of all pieces of size ranging from **1 to n**. Determine the maximum value obtainable by cutting up the rod and selling the pieces._ 218 | 219 | ### Problem solved 220 | | Platform | Title | Solution | Difficulty | 221 | |--------------|---------------- | --------------- |---------------| 222 | GEEKSFORGEEKS |[Reach a given score](https://practice.geeksforgeeks.org/problems/reach-a-given-score/0) | [View Solution](./DAY-09/Reach_a_given_score(GEEKSFORGEEKS).cpp) | Easy ||| 223 | GEEKSFORGEEKS |[Rod Cutting](https://practice.geeksforgeeks.org/problems/rod-cutting/0/?category) | [View Solution](./DAY-09/Rod_Cutting(GEEKSFORGEEKS).cpp) | Easy ||| 224 | 225 | ```diff 226 | ! solved Rod Cutting Problem 227 | ``` 228 | 229 | ##### 🎥Tutorial Link - https://www.youtube.com/watch?v=_gPcYovP7wc&list=PL_z_8CaSLPWekqhdCPmFohncHwz8TY2Go&index=14 230 | 231 | #### Set Goals. :star2: Say Prayers. :pray: Work Hard. :muscle: 232 | --- 233 | ## Day - 10 :closed_book: 234 | ### Solved: Coin Change Problem:moneybag: :moneybag: 235 | _**Problem Statement:** You are given a value N and array of coins. You need to find out the number of ways in which you can get value N from that coins. There is infinite supply of coins.(Unbounded Knapsack :wink:)_ 236 | 237 | ### Problem solved 238 | | Platform | Title | Solution | Difficulty | 239 | |--------------|---------------- | --------------- |---------------| 240 | GEEKSFORGEEKS |[Coin Change](https://practice.geeksforgeeks.org/problems/coin-change2448/1) | [View Solution](./DAY-10/Coin_Change(GEEKSFORGEEKS).cpp) | Medium ||| 241 | HACKERRANK |[The Coin Change Problem](https://www.hackerrank.com/challenges/coin-change/problem) | [View Solution](./DAY-10/The_Coin_Change_Problem(HACKERRANK).cpp) | Medium ||| 242 | LEETCODE |[Coin Change 2](https://leetcode.com/problems/coin-change-2/) | [View Solution](./DAY-10/Coin_Change_2(LEETCODE).cpp) | Medium ||| 243 | 244 | ```diff 245 | + solved Rod Cutting Problem 246 | ``` 247 | ##### 🎥Tutorial Link - https://www.youtube.com/watch?v=_gPcYovP7wc&list=PL_z_8CaSLPWekqhdCPmFohncHwz8TY2Go&index=15 248 | ##### 🎥Tutorial Link - https://www.youtube.com/watch?v=_gPcYovP7wc&list=PL_z_8CaSLPWekqhdCPmFohncHwz8TY2Go&index=16 249 | 250 | 251 | #### Prove Them Wrong :wink: :wink: 252 | 253 | 254 | --- 255 | ## Day - 11 :notebook: 256 | ### Solved: Maximize The Cut Segments Problem :pushpin: :pushpin: 257 | _**Problem Statement:** Given an integer **N** denoting the Length of a line segment. you need to cut the line segment in such a way that the cut length of a line segment each time is integer either **x**, **y**, or **z**. and after performing all cutting operations the total number of cut segments must be maximum._ 258 | 259 | ```diff 260 | - solved Coin Change 2 Problem 261 | ``` 262 | ### Problem solved 263 | | Platform | Title | Solution | Difficulty | 264 | |--------------|---------------- | --------------- |---------------| 265 | GEEKSFORGEEKS |[Maximize The Cut Segments](https://practice.geeksforgeeks.org/problems/cutted-segments/0) | [View Solution](./DAY-11/Maximize_The_Cut_Segments(GEEKSFORGEEKS).cpp) | Easy ||| 266 | LEETCODE |[Coin Change](https://leetcode.com/problems/coin-change/) | [View Solution](./DAY-11/Coin_Change(LEETCODE).cpp) | Medium ||| 267 | GEEKSFORGEEKS |[Number of Coins](https://practice.geeksforgeeks.org/problems/number-of-coins1824/1) | [View Solution](./DAY-11/Number_of_Coins(GEEKSFORGEEKS).cpp) | Medium ||| 268 |
269 | Important Tip -  This is a unique problem of Knapsack Family. 
270 | In this, we have to initialize 2nd row as well.
271 | 
272 | ```cpp 273 | for(int i =1; i` :laughing: :laughing: 351 | --- 352 | ## Day - 16 :closed_book: 353 | ### Solved: Printing LCS :pushpin: :pushpin: 354 | 355 | _**Problem Statement:** Given two strings **s1** and **s2**, print the longest subsequence present in both of them._ 356 | It's a unique question as in this question we need to traverse back into our DP array and check for positions where characters are matching 357 | ```cpp 358 | string s1 = "coding"; 359 | string s2 = "cubing"; 360 | -------------------------- 361 | dp array 362 | c o d i n g 363 | 0 0 0 0 0 0 0 364 | \ 365 | c 0 1 1 1 1 1 1 366 | | 367 | u 0 1 1 1 1 1 1 368 | | 369 | b 0 1-1-1 1 1 1 370 | \ 371 | i 0 1 1 1 2 2 2 372 | \ 373 | n 0 1 1 1 2 3 3 374 | \ 375 | g 0 1 1 1 2 3 4 376 | --------------------------- 377 | output => cing 378 | ``` 379 | 380 | ```diff 381 | @@ solved the Printing LCS. @@ 382 | ``` 383 | ### Problem solved 384 | | Platform | Title | Solution | Difficulty | 385 | |--------------|---------------- | --------------- |---------------| 386 | HACKERRANK|[The Longest Common Subsequence](https://www.hackerrank.com/challenges/dynamic-programming-classics-the-longest-common-subsequence/problem) | [View Solution](./DAY-16/The_Longest_Common_Subsequence(HACKERRANK).cpp) | Medium ||| 387 | 388 | #### I'd Rate My Programming Puns C++ :smirk: :smirk: 389 | 390 | --- 391 | ## Day - 17 :notebook: 392 | ### Solved: Shortest Common Supersequence :pushpin: :pushpin: 393 | 394 | _**Problem Statement:** Given two strings **str1** and **str2**, find the length of the smallest string which has both, **str1** and **str2** as its sub-sequences._ 395 | 396 | 397 | ```diff 398 | ! solved Shortest Common Supersequence. 399 | ``` 400 | ### Problem solved 401 | | Platform | Title | Solution | Difficulty | 402 | |--------------|---------------- | --------------- |---------------| 403 | GEEKSFORGEEKS |[Shortest Common Supersequence](https://practice.geeksforgeeks.org/problems/shortest-common-supersequence/0#) | [View Solution](./DAY-17/Shortest_Common_Supersequence(GEEKSFORGEEKS).cpp) | Easy||| 404 | 405 | #### A rabbit aims for the moon :confused: 406 | 407 | 408 | --- 409 | ## Day - 18 :orange_book: 410 | ### Solved: Minimum Number of Deletions and Insertions :pushpin: :pushpin: 411 | 412 | _**Problem Statement:** Given two strings **str1** and **str2**. Your task is to convert **str1** to **str2**. You can only perform two types of operations that are **remove** or **insert**. Find the minimum number of operations required to convert **str1** into **str2**._ 413 | 414 | 415 | ```diff 416 | + solved Minimum Number of Deletions and Insertions. It's a simpler version of classical Edit Distance Problem 417 | ``` 418 | ### Problem solved 419 | | Platform | Title | Solution | Difficulty | 420 | |--------------|---------------- | --------------- |---------------| 421 | GEEKSFORGEEKS |[Minimum number of deletions and insertions](https://practice.geeksforgeeks.org/problems/minimum-number-of-deletions-and-insertions0209/1) | [View Solution](./DAY-18/Minimum_number_of_deletions_and_insertions(GEEKSFORGEEKS).cpp) | Easy||| 422 | 423 | #### Please Don't Let Your Dreams Die :pray: :pray: 424 | 425 | 426 | --- 427 | ## Day - 19 :blue_book: 428 | ### Solved: Longest Palindromic Subsequence :pushpin: :pushpin: 429 | 430 | _**Problem Statement:** Given a string **str1**. Your task is to find the Longest Palindromic Subsequence of that string **s1**.._ 431 | 432 | 433 | ```diff 434 | - solved Longest Palindromic Subsequence. 435 | ``` 436 | ### Problem solved 437 | | Platform | Title | Solution | Difficulty | 438 | |--------------|---------------- | --------------- |---------------| 439 | GEEKSFORGEEKS |[Longest Palindromic Subsequence](https://practice.geeksforgeeks.org/problems/longest-palindromic-subsequence/0#) | [View Solution](./DAY-19/Longest_Palindromic_Subsequence(GEEKSFORGEEKS).cpp) | Easy||| 440 | LEETCODE |[Longest Palindromic Subsequence](https://leetcode.com/problems/longest-palindromic-subsequence/) | [View Solution](./DAY-19/Longest_Palindromic_Subsequence(LEETCODE).cpp) | Medium||| 441 | INTERVIEWBIT |[Longest Palindromic Subsequence](https://www.interviewbit.com/problems/longest-palindromic-subsequence/) | [View Solution](./DAY-19/Longest_Palindromic_Subsequence(INTERVIEWBIT).cpp) | Medium||| 442 | 443 | #### Be Different. Be Awesome. :wink: :dizzy: 444 | 445 | --- 446 | ## Day - 20 :green_book: 447 | ### Solved: Minimum Deletions to make string Palindromic :pushpin: :pushpin: 448 | 449 | _**Problem Statement:** Given a string of **s1**. Your task is to remove or delete minimum number of characters from the string so that the resultant string is palindrome._ 450 | 451 | 452 | ```diff 453 | @@ solved Minimum Deletions to make string Palindromic. @@ 454 | ``` 455 | ### Problem solved 456 | | Platform | Title | Solution | Difficulty | 457 | |--------------|---------------- | --------------- |---------------| 458 | GEEKSFORGEEKS |[Minimum Deletions](https://practice.geeksforgeeks.org/problems/minimum-deletitions/0) | [View Solution](./DAY-20/Minimum_Deletions(GEEKSFORGEEKS).cpp) | Easy||| 459 | 460 | #### Stay Patient. Trust your journey. :relieved: :round_pushpin: 461 | 462 | --- 463 | ## Day - 21 :ledger: 464 | ### Solved: Printing Shortest Common Supersequence :pushpin: :pushpin: 465 | 466 | _**Problem Statement:** Given two strings **str1** and **str2**, print the smallest string which has both, **str1** and **str2** as its sub-sequences._ 467 | 468 | 469 | ```diff 470 | ! solved Printing Shortest Common Supersequence. 471 | ``` 472 | ### Problem solved 473 | | Platform | Title | Solution | Difficulty | 474 | |--------------|---------------- | --------------- |---------------| 475 | LEETCODE |[Shortest Common Supersequence](https://leetcode.com/problems/shortest-common-supersequence/) | [View Solution](./DAY-21/Shortest_Common_Supersequence(LEETCODE).cpp) | Hard||| 476 | 477 | #### :yellow_heart: 21-Days Streak. :blue_heart: 21-Days of CP. :purple_heart: 21-Days of DP :green_heart: 21-Days of OP :heart: 478 | 479 | --- 480 | ## Day - 22 :closed_book: 481 | ### Solved: Longest Repeating Subsequence :pushpin: :pushpin: 482 | 483 | _**Problem Statement:** Given a string **s**, find the length of the longest repeating SubSequence such that the two subsequences don’t have the same string character in the same position, i.e., any i’th character in the two subsequences shouldn’t have the same index in the original string._ 484 | ```diff 485 | + solved Longest Repeating Subsequence. 486 | ``` 487 | ### Problem solved 488 | | Platform | Title | Solution | Difficulty | 489 | |--------------|---------------- | --------------- |---------------| 490 | GEEKSFORGEEKS |[Longest Repeating Subsequence](https://practice.geeksforgeeks.org/problems/longest-repeating-subsequence/0#) | [View Solution](./DAY-22/Longest_Repeating_Subsequence(GEEKSFORGEEKS).cpp) | Easy||| 491 | INTERVIEWBIT|[Repeating Sub-Sequence](https://www.interviewbit.com/problems/repeating-subsequence/) | [View Solution](./DAY-22/Repeating_Sub-Sequence(INTERVIEWBIT).cpp) | Medium||| 492 | 493 | #### Hey ! We are still at Day-0! :relieved: :relieved: 494 | 495 | --- 496 | ## Day - 23 :notebook: 497 | ### Solved: Is Subsequence ? :pushpin: :pushpin: 498 | _**Problem Statement:** Given two strings **str1** and **str2**, check if **str1** is subsequence of **str2**.._ 499 | 500 | ```diff 501 | - solved Is Subsequence ? 502 | ``` 503 | 504 |
505 | Important Tip -  This question can be solved in O(n) time complexity without DP also. But still DP  is Awesome.
506 | 
507 | ```cpp 508 | Approach: Simpply we can apply LCS on both str1 and str2, 509 | and then we can check that the Length of LCS is equals to s1 or not. 510 | 511 | if(dp[n][m] == s1.length()) 512 | return true; 513 | else 514 | return false; 515 | ``` 516 | 517 | #### LIVE :dizzy: LAUGH :laughing: CODE :computer: 518 | 519 | --- 520 | ## Day - 24 :orange_book: 521 | ### Solved: Minimum Insertion Steps to Make a String Palindrome :pushpin: :pushpin: 522 | 523 | _**Problem Statement:** Given a string **s**, find the minimum number of insertion operations you could perform on the string to make the string palindromic._ 524 | ```diff 525 | @@ solved Minimum Insertion Steps to Make a String Palindrome. @@ 526 | ``` 527 | ### Problem solved 528 | | Platform | Title | Solution | Difficulty | 529 | |--------------|---------------- | --------------- |---------------| 530 | GEEKSFORGEEKS |[Form a palindrome](https://practice.geeksforgeeks.org/problems/form-a-palindrome/0) | [View Solution](./DAY-24/Form_a_palindrome(GEEKSFORGEEKS).cpp) | Medium||| 531 | LEETCODE|[Minimum Insertion Steps to Make a String Palindrome](https://leetcode.com/problems/minimum-insertion-steps-to-make-a-string-palindrome/) | [View Solution](./DAY-24/Minimum_Insertion_Steps_to_Make_a_String_Palindrome(LEETCODE).cpp) | Hard||| 532 | SPOJ|[IOIPALIN - Palindrome 2000](https://www.spoj.com/problems/IOIPALIN/) | [View Solution](./DAY-24/Palindrome_2000(SPOJ).cpp) | Medium||| 533 | 534 | #### Its Okay ! Try Again :muscle: :angel: 535 | 536 | --- 537 | 538 | ## Day - 25 :blue_book: 539 | ### Solved: Matrix Chain Multiplication (Recursive):pushpin: :pushpin: 540 | 541 | _**Problem Statement:** Given an array p[] which represents the chain of matrices such that the ith matrix Ai is of dimension p[i-1] x p[i]. We need to write a function that should return the minimum number of multiplications needed to multiply the chain._ 542 | 543 | **Optimal Substructure Solution:** 544 | In this approach, we can place parenthesis at all possible places and then calculate the cost, and finally, we can find the minimum value. _**For example, if the given chain is of 4 matrices. let the chain be ABCD, then there are 3 ways to solve: (A)(BCD), (AB)(CD), and (ABC)(D). So when we place a set of parentheses, we divide the problem into subproblems of smaller size. Therefore, the problem has optimal substructure property and can be easily solved using recursion.**_ 545 | ```diff 546 | ! solved Matrix Chain Multiplication (Recursive). 547 | ``` 548 | 549 | 550 | #### If you avoid difficult things, great things will avoid you :dizzy: :green_heart: 551 | 552 | --- 553 | 554 | ## Day - 26:green_book: 555 | ### Solved: Matrix Chain Multiplication (Memoisation):pushpin: :pushpin: 556 | 557 | _**Problem Statement:** Given an array p[] which represents the chain of matrices such that the ith matrix Ai is of dimension p[i-1] x p[i]. We need to write a function that should return the minimum number of multiplications needed to multiply the chain._ 558 | 559 | Just 4 more lines of code....and 💥💥💥 now your code is Memoised. :heart: 560 | 561 | ```diff 562 | + solved Matrix Chain Multiplication (Memoisation). 563 | ``` 564 | 565 | 566 | #### Those Who Cannot Remember the Past are Condemned to Repeat It :laughing: :laughing: ~ Dynamic Programming :dizzy: 567 | 568 | --- 569 | 570 | --------------------------------------------------------------------------------