diff --git a/Dynamic Programming/Coin_change/a.exe b/Dynamic Programming/Coin_change/a.exe new file mode 100644 index 0000000..36a2d2e Binary files /dev/null and b/Dynamic Programming/Coin_change/a.exe differ diff --git a/Dynamic Programming/Coin_change/max_ways.cpp b/Dynamic Programming/Coin_change/max_ways.cpp new file mode 100644 index 0000000..5b94346 --- /dev/null +++ b/Dynamic Programming/Coin_change/max_ways.cpp @@ -0,0 +1,43 @@ +//Program to count how many ways exist that an amount can be broken into a number of denominations. +#include +using namespace std; +int t[100][1000]; +int counting(int arr[], int sum,int n) //unbounded subset sum +{ + for(int i=0;ij) + t[i][j]=t[i-1][j]; + + } + } +return t[n][sum]; +} +int main() +{ int coin[] = {1,2,3}; //example cases + int amount= 10; + int n = sizeof(coin) / sizeof(coin[0]); + memset(t,0,sizeof(t)); + int count= counting(coin,amount,n); + cout< +using namespace std; +int t[100][1000]; +int counting(int arr[], int sum,int n) //unbounded subset sum +{ + for(int i=0;ij) + t[i][j]=t[i-1][j]; + + } + } +return t[n][sum]; +} +int main() +{ int coin[] = {9, 6, 5, 1}; //example cases + int amount= 11; + int n = sizeof(coin) / sizeof(coin[0]); + memset(t,0,sizeof(t)); + int count= counting(coin,amount,n); + cout< +using namespace std; +int t[100][1000]; +int counting(int arr[], int sum,int n) +{ + for(int i=0;ij) + t[i][j]=t[i-1][j]; + + } + } +return t[n][sum]; +} +int main() +{ int arr[] = { 2,3,4,7}; //example cases + int sum= 5; + int n = sizeof(arr) / sizeof(arr[0]); + memset(t,0,sizeof(t)); + int count= counting(arr,sum,n); + cout< +using namespace std; +bool t[100][1000]; //t[n+1][sum+1] +bool equalsum(int arr[], int n) +{ int sum=0; + for(int i=0;ij) + t[i][j]=t[i-1][j]; + + } + } +return t[n][sum]; +} +int main() +{ int arr[] = { 2,4 }; //example cases + int n = sizeof(arr) / sizeof(arr[0]); + memset(t,-false,sizeof(t)); + if(equalsum( arr,n)) + printf("YES"); + else + { + printf("NO"); + } + + return 0; +} \ No newline at end of file diff --git a/Dynamic Programming/Knapsack_zero_one/a.exe b/Dynamic Programming/Knapsack_zero_one/a.exe new file mode 100644 index 0000000..9e5e378 Binary files /dev/null and b/Dynamic Programming/Knapsack_zero_one/a.exe differ diff --git a/Dynamic Programming/Knapsack_zero_one/zero_one_knapsack_dp.cpp b/Dynamic Programming/Knapsack_zero_one/zero_one_knapsack_dp.cpp new file mode 100644 index 0000000..fa0567b --- /dev/null +++ b/Dynamic Programming/Knapsack_zero_one/zero_one_knapsack_dp.cpp @@ -0,0 +1,39 @@ +//dp code maximum profit +#include +using namespace std; +int t[100][1000]; +int max(int a, int b) { return (a > b) ? a : b; } +int knapsack(int weight[],int val[], int c,int n) +{ + for(int i=0;ij) + t[i][j]=t[i-1][j]; + + } + } +return t[n][c]; +} +int main() +{ int val[] = { 60, 100, 120 }; //example cases + int wt[] = { 10, 20, 30 }; + int W = 50; + int n = sizeof(val) / sizeof(val[0]); + memset(t,-1,sizeof(t)); + cout << knapsack( wt, val,W, n); + return 0; +} \ No newline at end of file diff --git a/Dynamic Programming/Knapsack_zero_one/zero_one_knapsack_mem.cpp b/Dynamic Programming/Knapsack_zero_one/zero_one_knapsack_mem.cpp new file mode 100644 index 0000000..e1ece14 --- /dev/null +++ b/Dynamic Programming/Knapsack_zero_one/zero_one_knapsack_mem.cpp @@ -0,0 +1,34 @@ +//memoization code +#include +using namespace std; +int t[100][1000]; +int max(int a, int b) { return (a > b) ? a : b; } +int knapsack(int weight[],int val[], int c,int n) +{ + if(n==0||c==0) + { + return 0; + } + if(t[n][c]!=-1) + { + return t[n][c]; + } + + + if(weight[n-1]<=c) + { + return t[n][c]= max((val[n-1]+ knapsack(weight,val,c-weight[n-1],n-1)), knapsack(weight,val,c, n-1)); + } + else if (weight[n-1]>c) + return t[n][c]=knapsack(weight,val,c,n-1); + return 0; +} +int main() +{ int val[] = { 60, 100, 120 }; //example cases + int wt[] = { 10, 20, 30 }; + int W = 50; + memset(t,-1,sizeof(t)); + int n = sizeof(val) / sizeof(val[0]); + cout << knapsack( wt, val,W, n); + return 0; +} \ No newline at end of file diff --git a/Dynamic Programming/Knapsack_zero_one/zero_one_knapsack_rec.cpp b/Dynamic Programming/Knapsack_zero_one/zero_one_knapsack_rec.cpp new file mode 100644 index 0000000..f4d9a8b --- /dev/null +++ b/Dynamic Programming/Knapsack_zero_one/zero_one_knapsack_rec.cpp @@ -0,0 +1,26 @@ +//recursive code maximum profit +#include +using namespace std; +int max(int a, int b) { return (a > b) ? a : b; } +int knapsack(int weight[],int val[], int c,int n) +{ + if(n==0||c==0) + { + return 0; + } + if(weight[n-1]<=c) + { + return max((val[n-1]+ knapsack(weight,val,c-weight[n-1],n-1)), knapsack(weight,val,c, n-1)); + } + else if (weight[n-1]>c) + return knapsack(weight,val,c,n-1); + return 0; +} +int main() +{ int val[] = { 60, 100, 120 }; //example cases + int wt[] = { 10, 20, 30 }; + int W = 50; + int n = sizeof(val) / sizeof(val[0]); + cout << knapsack( wt, val,W, n); + return 0; +} \ No newline at end of file diff --git a/Dynamic Programming/Largest_Palindromic_Subsequence/a.exe b/Dynamic Programming/Largest_Palindromic_Subsequence/a.exe new file mode 100644 index 0000000..176b187 Binary files /dev/null and b/Dynamic Programming/Largest_Palindromic_Subsequence/a.exe differ diff --git a/Dynamic Programming/Largest_Palindromic_Subsequence/pal.cpp b/Dynamic Programming/Largest_Palindromic_Subsequence/pal.cpp new file mode 100644 index 0000000..10b680e --- /dev/null +++ b/Dynamic Programming/Largest_Palindromic_Subsequence/pal.cpp @@ -0,0 +1,49 @@ +/*Longest Palindromic Subsequence +Given a sequence, find the length of the longest palindromic subsequence in it. +Example : +Input:"bbbab" +Output:4*/ +#include +#include +#include +using namespace std; +int t[100][1000]; + +int LCS(string X,string Y, int m,int n) +{ + for (int i = 0; i < m+1; ++i) + { + for (int j = 0; j < n+1; ++j) + { + if(i==0||j==0) + t[i][j]=0; + } + } + +for (int i = 1; i < m+1; ++i) +{ + for(int j = 1; j < n+1; ++j) + { + if(X[i-1]==Y[j-1]) + { + t[i][j]=1+t[i-1][j-1]; + } + else + { + t[i][j]=max(t[i-1][j],t[i][j-1]); + } + } +} +return t[m][n]; +} +int main() +{ + string X="bbbab"; + string Y=X; + reverse(Y.begin(), Y.end()); + int m=X.length(); + int n = Y.length(); + memset(t,-1,sizeof(t)); + cout< +#include +#include +using namespace std; +int t[100][1000]; + +int LCS(string X,string Y, int m,int n) +{ + for (int i = 0; i < m+1; ++i) + { + for (int j = 0; j < n+1; ++j) + { + if(i==0||j==0) + t[i][j]=0; + } + } + +for (int i = 1; i < m+1; ++i) +{ + for(int j = 1; j < n+1; ++j) + { + if(X[i-1]==Y[j-1]) + { + t[i][j]=1+t[i-1][j-1]; + } + else + { + t[i][j]=max(t[i-1][j],t[i][j-1]); + } + } +} +return t[m][n]; +} +int main() +{ + string X="sujoy"; + string Y="sujoyy"; + int m=X.length(); + int n = Y.length(); + memset(t,-1,sizeof(t)); + cout< +#include +using namespace std; +int t[100][1000]; +int LCS(string X,string Y, int m,int n) +{ + + if(m==0||n==0) + return 0; + + + if(t[m][n]!=-1) + { + return t[m][n]; + } + + if(X[m-1]==Y[n-1]) + { + return t[m][n]= 1+LCS(X,Y,m-1,n-1); + } + else + { + return t[m][n]= max(LCS(X,Y,m,n-1),LCS(X,Y, m-1, n-1)); + } + + +} +int main() +{ + string X="sujffy"; + string Y="sujoyyyy"; + int m= X.length(); + int n = Y.length(); + memset(t,-1,sizeof(t)); + cout< +#include +#include +using namespace std; +int LCS(string X,string Y, int m,int n) +{ + if(m==0||n==0) + return 0; + + +if(X[m-1]==Y[n-1]) +{ + return 1+LCS(X,Y,m-1,n-1); +} +else +{ + return max(LCS (X,Y,m,n-1),LCS(X,Y, m-1, n-1)); +} + +} +int main() +{ + string X="sujoy"; + string Y="sujoyy"; + int m=X.length(); + int n = Y.length(); + cout< +#include +#include +using namespace std; +int t[100][1000]; + +int LCS(string X,string Y, int m,int n) +{ + for (int i = 0; i < m+1; ++i) + { + for (int j = 0; j < n+1; ++j) + { + if(i==0||j==0) + t[i][j]=0; + } + } + +for (int i = 1; i < m+1; ++i) +{ + for(int j = 1; j < n+1; ++j) + { + if(X[i-1]==Y[j-1]) + { + t[i][j]=1+t[i-1][j-1]; + } + else + { + t[i][j]=0; + } + } +} +auto max= t[0][0]; + +for (int i = 0; i < m+1; ++i) +{ + for (int j = 0; j < n+1; ++j) + { + if(max +using namespace std; +int t[1001][1001]; +int solve(int arr[],int i,int j) +{ + if(i>=j) + { + return 0; + } + if(t[i][j]!=-1) + { + return t[i][j]; + } + int min = INT_MAX; + for(int k=i;ktemp) + { + min=temp; + } + + } + t[i][j]=min; + return t[i][j]; + +} +int main() +{ + memset(t,-1,sizeof(t)); + int arr[]={40,30,20,30}; + cout< +using namespace std; +int solve(int arr[],int i,int j) +{ + if(i>=j) + { + return 0; + } + int min = INT_MAX; + for(int k=i;ktemp) + { + min=temp; + } + + } + return min; + +} +int main() +{ + int arr[]={40,30,20,30}; + int i=1,j=3; + cout< +#include +#include +using namespace std; +int t[100][1000]; + +int LCS(string X,string Y, int m,int n) +{ + for (int i = 0; i < m+1; ++i) + { + for (int j = 0; j < n+1; ++j) + { + if(i==0||j==0) + t[i][j]=0; + } + } + +for (int i = 1; i < m+1; ++i) +{ + for(int j = 1; j < n+1; ++j) + { + if(X[i-1]==Y[j-1]) + { + t[i][j]=1+t[i-1][j-1]; + } + else + { + t[i][j]=max(t[i-1][j],t[i][j-1]); + } + } +} +return t[m][n]; +} +int main() +{ + string X="aebcbda"; + string Y=X; + reverse(Y.begin(), Y.end()); + int m=X.length(); + int n = Y.length(); + memset(t,-1,sizeof(t)); + int d= m-LCS(X,Y,m,n); + cout< +#include +#include +using namespace std; +int t[100][1000]; + +int LCS(string X,string Y, int m,int n) +{ + for (int i = 0; i < m+1; ++i) + { + for (int j = 0; j < n+1; ++j) + { + if(i==0||j==0) + t[i][j]=0; + } + } + +for (int i = 1; i < m+1; ++i) +{ + for(int j = 1; j < n+1; ++j) + { + if(X[i-1]==Y[j-1]) + { + t[i][j]=1+t[i-1][j-1]; + } + else + { + t[i][j]=max(t[i-1][j],t[i][j-1]); + } + } +} +return t[m][n]; +} +int main() +{ + string X="aebcbda"; + string Y=X; + reverse(Y.begin(), Y.end()); + int m=X.length(); + int n = Y.length(); + memset(t,-1,sizeof(t)); + int d= m-LCS(X,Y,m,n); + cout< +using namespace std; +int t[100][1000]; +int counting(int arr[], int sum,int n) //using count of subset sum +{ + for(int i=0;ij) + t[i][j]=t[i-1][j]; + + } + } +return t[n][sum]; +} +int main() +{ int arr[] = {1,1,2,3}; //example cases + int diff= 1; + + int n = sizeof(arr) / sizeof(arr[0]); + int range=0,sum; + for(int i=0;i +#include +#include +using namespace std; +int t[1001][1001]; +bool ispalindrome(string s, int i, int j) +{ + if(i == j) { + return true; + } + + if(i > j) { + return true; + } + + while( i < j) { + if(s[i] != s[j]) { + return false; + } + + i++; + j--; + } + return true; + + } +int solve(string s,int i,int j) +{ + if(i>=j) + { + return 0; + } + if(ispalindrome(s,i,j)) + { + return 0; + } + if(t[i][j]!=-1) + { + return t[i][j]; + } + int min = INT_MAX; int left,right; + for(int k=i;ktemp) + { + min=temp; + } + + } + return t[i][j]=min; + +} +int main() +{ + string s= "abcde"; + memset(t,-1,sizeof(t)); + cout< +#include +#include +using namespace std; +bool ispalindrome(string s, int i, int j) { + if(i == j) { + return true; + } + + if(i > j) { + return true; + } + + while( i < j) { + if(s[i] != s[j]) { + return false; + } + + i++; + j--; + } + return true; + + } +int solve(string s,int i,int j) +{ + if(i>=j) + { + return 0; + } + if(ispalindrome(s,i,j)) + { + return 0; + } + int min = INT_MAX; + for(int k=i;ktemp) + { + min=temp; + } + + } + return min; + +} +int main() +{ + string s= "abcde"; + cout< +#include +#include +using namespace std; +int t[100][1000]; + +int LCS(string X,string Y, int m,int n) +{ + for (int i = 0; i < m+1; ++i) + { + for (int j = 0; j < n+1; ++j) + { + if(i==0||j==0) + t[i][j]=0; + } + } + +for (int i = 1; i < m+1; ++i) +{ + for(int j = 1; j < n+1; ++j) + { + if(X[i-1]==Y[j-1]) + { + t[i][j]=1+t[i-1][j-1]; + } + else + { + t[i][j]=max(t[i-1][j],t[i][j-1]); + } + } +} +return t[m][n]; +} +int main() +{ + string X="AGGTAB"; + string Y="GXTXAYB"; + int m=X.length(); + int n = Y.length(); + memset(t,-1,sizeof(t)); + LCS(X,Y,m,n); + int i=m,j=n; + string s; + while (i>0 && j>0) + { + if(X[i-1]==Y[j-1]) + { + s.push_back(X[i-1]); + i--;j--; + } + else + { + if(t[i][j-1]>t[i-1][j]) + { + j--; + } + else + { + i--; + } + + } + + } + reverse(s.begin(),s.end()); + cout< +using namespace std; +int t[100][1000]; +int max(int a, int b) { return (a > b) ? a : b; } +int knapsack(int weight[],int val[], int c,int n) //unbounded +{ + for(int i=0;ij) + t[i][j]=t[i-1][j]; + + } + } +return t[n][c]; +} +int main() +{ int price[] = { 60, 100, 120 }; //example cases + int length[] = { 10, 20, 30 }; + int L = 50; + int n = sizeof(length) / sizeof(length[0]); + memset(t,-1,sizeof(t)); + cout << knapsack( length, price,L, n); + return 0; +} \ No newline at end of file diff --git a/Dynamic Programming/Shortest_Common_SuperSequence/a.exe b/Dynamic Programming/Shortest_Common_SuperSequence/a.exe new file mode 100644 index 0000000..07edec1 Binary files /dev/null and b/Dynamic Programming/Shortest_Common_SuperSequence/a.exe differ diff --git a/Dynamic Programming/Shortest_Common_SuperSequence/short.cpp b/Dynamic Programming/Shortest_Common_SuperSequence/short.cpp new file mode 100644 index 0000000..a4a3fec --- /dev/null +++ b/Dynamic Programming/Shortest_Common_SuperSequence/short.cpp @@ -0,0 +1,51 @@ +/*Given two strings str1 and str2, find the shortest string that has both str1 and str2 as subsequences. +Examples : + +Input: str1 = "geek", str2 = "eke" +Output: 5 + +Input: str1 = "AGGTAB", str2 = "GXTXAYB" +Output: 9 */ +#include +#include +#include +using namespace std; +int t[100][1000]; + +int LCS(string X,string Y, int m,int n) +{ + for (int i = 0; i < m+1; ++i) + { + for (int j = 0; j < n+1; ++j) + { + if(i==0||j==0) + t[i][j]=0; + } + } + +for (int i = 1; i < m+1; ++i) +{ + for(int j = 1; j < n+1; ++j) + { + if(X[i-1]==Y[j-1]) + { + t[i][j]=1+t[i-1][j-1]; + } + else + { + t[i][j]=max(t[i-1][j],t[i][j-1]); + } + } +} +return t[m][n]; +} +int main() +{ + string X="geek"; + string Y="eke"; + int m=X.length(); + int n = Y.length(); + memset(t,-1,sizeof(t)); + cout<<(m+n-LCS(X,Y,m,n))< +using namespace std; +bool t[100][1000]; +bool subsetsum(int arr[], int sum,int n) +{ + for(int i=0;ij) + t[i][j]=t[i-1][j]; + + } + } +return t[n][sum]; +} +int main() +{ int arr[] = { 2,3,6,10 }; //example cases + int sum= 18436; + int n = sizeof(arr) / sizeof(arr[0]); + memset(t,false,sizeof(t)); + if(subsetsum( arr,sum,n)) + printf("YES"); + else + { + printf("NO"); + } + + return 0; +} \ No newline at end of file diff --git a/Dynamic Programming/Target_sum/a.exe b/Dynamic Programming/Target_sum/a.exe new file mode 100644 index 0000000..6a41fcc Binary files /dev/null and b/Dynamic Programming/Target_sum/a.exe differ diff --git a/Dynamic Programming/Target_sum/target.cpp b/Dynamic Programming/Target_sum/target.cpp new file mode 100644 index 0000000..cc7fec3 --- /dev/null +++ b/Dynamic Programming/Target_sum/target.cpp @@ -0,0 +1,52 @@ +//Counting the numbere of subsets of a given array with a given difference. //LEETCODE PROBLEM. +#include +using namespace std; +int t[100][1000]; +int counting(int arr[], int sum,int n) //using count of subset sum +{ + for(int i=0;ij) + t[i][j]=t[i-1][j]; + + } + } +return t[n][sum]; +} +int main() +{ int arr[] = {1,1,2,3}; //example cases + int diff= 1; + + int n = sizeof(arr) / sizeof(arr[0]); + int range=0,sum; + for(int i=0;i +using namespace std; +int t[100][1000]; +int max(int a, int b) { return (a > b) ? a : b; } +int knapsack(int weight[],int val[], int c,int n) +{ + for(int i=0;ij) + t[i][j]=t[i-1][j]; + + } + } +return t[n][c]; +} +int main() +{ int val[] = { 60, 100, 120 }; //example cases + int wt[] = { 10, 20, 30 }; + int W = 50; + int n = sizeof(val) / sizeof(val[0]); + memset(t,-1,sizeof(t)); + cout << knapsack( wt, val,W, n); + return 0; +} \ No newline at end of file diff --git a/Dynamic Programming/longest_repeating_subsequence/a.exe b/Dynamic Programming/longest_repeating_subsequence/a.exe new file mode 100644 index 0000000..456912e Binary files /dev/null and b/Dynamic Programming/longest_repeating_subsequence/a.exe differ diff --git a/Dynamic Programming/longest_repeating_subsequence/long.cpp b/Dynamic Programming/longest_repeating_subsequence/long.cpp new file mode 100644 index 0000000..e0137f5 --- /dev/null +++ b/Dynamic Programming/longest_repeating_subsequence/long.cpp @@ -0,0 +1,51 @@ +/*Given a string, print the longest repeating subsequence such that the two subsequence don’t have same string character at same position, i.e., any i’th character in the two subsequences shouldn’t have the same index in the original string. +Example: +Input: str = "aab" +Output: "a" +The two subsequence are 'a'(first) and 'a' +(second). Note that 'b' cannot be considered +as part of subsequence as it would be at same +index in both.*/ +#include +#include +#include +using namespace std; +int t[100][1000]; + +int LCS(string X,string Y, int m,int n) +{ + for (int i = 0; i < m+1; ++i) + { + for (int j = 0; j < n+1; ++j) + { + if(i==0||j==0) + t[i][j]=0; + } + } + +for (int i = 1; i < m+1; ++i) +{ + for(int j = 1; j < n+1; ++j) + { + if(X[i-1]==Y[j-1]&& i!=j) + { + t[i][j]=1+t[i-1][j-1]; + } + else + { + t[i][j]=max(t[i-1][j],t[i][j-1]); + } + } +} +return t[m][n]; +} +int main() +{ + string X="aabbdefgcc"; + string Y=X; + int m=X.length(); + int n = Y.length(); + memset(t,-1,sizeof(t)); + cout< +#include +#include +using namespace std; +int t[100][1000]; + +int LCS(string X,string Y, int m,int n) +{ + for (int i = 0; i < m+1; ++i) + { + for (int j = 0; j < n+1; ++j) + { + if(i==0||j==0) + t[i][j]=0; + } + } + +for (int i = 1; i < m+1; ++i) +{ + for(int j = 1; j < n+1; ++j) + { + if(X[i-1]==Y[j-1]) + { + t[i][j]=1+t[i-1][j-1]; + } + else + { + t[i][j]=max(t[i-1][j],t[i][j-1]); + } + } +} +return t[m][n]; +} +int main() +{ + string X="heap"; + string Y="pea"; + int m=X.length(); + int n = Y.length(); + memset(t,-1,sizeof(t)); + int ins = n-LCS(X,Y,m,n); + int del = m-LCS(X,Y,m,n); + cout< +using namespace std; +bool t[100][1000]; +bool subsetsum(int arr[], int sum,int n) //use of the subset-sum problem function +{ + for(int i=0;ij) + t[i][j]=t[i-1][j]; + + } + } +return t[n][sum]; +} +int main() +{ int arr[] = {1,2,7,8,9 }; //example cases + int range=0; + int n = sizeof(arr) / sizeof(arr[0]); + memset(t,false,sizeof(t)); + for(int i=0;i v; + for(int i=0;i<=range/2;i++) + { + if(t[n][i]) + { + v.push_back(i); + } + } + for(int i=0;i +#include +#include +using namespace std; +int t[100][1000]; + +int LCS(string X,string Y, int m,int n) +{ + for (int i = 0; i < m+1; ++i) + { + for (int j = 0; j < n+1; ++j) + { + if(i==0||j==0) + t[i][j]=0; + } + } + +for (int i = 1; i < m+1; ++i) +{ + for(int j = 1; j < n+1; ++j) + { + if(X[i-1]==Y[j-1]&& i!=j) + { + t[i][j]=1+t[i-1][j-1]; + } + else + { + t[i][j]=max(t[i-1][j],t[i][j-1]); + } + } +} +return t[m][n]; +} +int main() +{ + string X="AXY"; + int p=X.length(); + string Y="ADX"; + X=Y+X; + Y=X; + int m=X.length(); + int n = Y.length(); + memset(t,-1,sizeof(t)); + if(LCS(X,Y,m,n)>=p) + { + cout<<"True"<