Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 35bb941

Browse files
Merge pull request #102 from jineshparakh/master
Added Dynamic Programming and Recursion Problems
2 parents 5a93c09 + 6bd698b commit 35bb941

10 files changed

+517
-0
lines changed

‎10- Recursion & Backtracking/LCS.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
#define ll long long
4+
5+
ll LCS(string X, string Y, ll n, ll m) {
6+
/*
7+
Base case:
8+
If either one of the two strings becomes null string, we cannot choose
9+
more from it.
10+
So return 0.
11+
*/
12+
if (n == 0 || m == 0)
13+
return 0LL;
14+
/*
15+
Choice Diagram:
16+
A. If the n-1th and m-1th character of both strings is equal, we select it and recur
17+
for n-1 and m-1
18+
B. If they are not equal, we have two choices.
19+
1. Recur for n-1,m
20+
2. Recur for n,m-1
21+
We have to find the LCS, so we will choose the max among the two cases.
22+
*/
23+
if (X[n - 1] == Y[m - 1])
24+
return LCS(X, Y, n - 1, m - 1) + 1LL;
25+
else {
26+
return max(LCS(X, Y, n - 1, m), LCS(X, Y, n, m - 1));
27+
}
28+
}
29+
int main() {
30+
31+
/*
32+
LONGEST COMMON SUBSEQUENCE LENGTH
33+
Given two string X and Y, find the length of LCS between them.
34+
*/
35+
string X, Y; cin >> X >> Y;
36+
ll n = X.size(), m = Y.size();
37+
cout << LCS(X, Y, n, m) << endl;
38+
39+
/*
40+
Sample Input:
41+
AGGTAB
42+
GXTXAYB
43+
Output:
44+
4
45+
Sample Input:
46+
ABCDGH
47+
AEDFHR
48+
Output:
49+
3
50+
*/
51+
52+
return 0;
53+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
#define ll long long
4+
5+
ll findCountofSubsets(ll a[], ll n, ll S) {
6+
if (n == 0 && S == 0)
7+
return 1;
8+
if (n == 0 && S != 0)
9+
return 0;
10+
if (n != 0 && S == 0)
11+
return 1;
12+
if (a[n - 1] <= S) {
13+
return findCountofSubsets(a, n - 1, S) + findCountofSubsets(a, n - 1, S - a[n - 1]);
14+
}
15+
else return findCountofSubsets(a, n - 1, S);
16+
}
17+
int main() {
18+
19+
/*
20+
Given n as the size of array
21+
a[i] is the ith element of the array
22+
S is the requeired absolute difference between the two partitions of the array
23+
To find:
24+
Count the number of valid pairs of subsets such that the given difference
25+
is obtained
26+
*/
27+
ll n, S; cin >> n >> S;
28+
ll a[n];
29+
ll sum = 0;
30+
for(ll i = 0; i < n; i++) {
31+
cin >> a[i];
32+
sum += a[i];
33+
}
34+
if ((sum + S) & 1)
35+
cout << 0 << endl;
36+
else
37+
/*
38+
Why (sum+S)/2;
39+
sum(subset1)-sum(subset2)=difference(S);
40+
sum(subset1)+sub(subset2)=sumofArray(sum);
41+
Add these two
42+
sum(subset1)=(S+sum)/2
43+
*/
44+
cout << findCountofSubsets(a, n, (sum + S) / 2) << endl;
45+
46+
return 0;
47+
}
48+
49+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
#define ll long long
4+
5+
ll countSubsets(ll a[], ll n, ll S) {
6+
if (n == 0 && S == 0)
7+
return 1;
8+
else if (n != 0 && S == 0)
9+
return 1;
10+
else if (n == 0 && S != 0)
11+
return 0;
12+
if (a[n - 1] <= S) {
13+
return countSubsets(a, n - 1, S) + countSubsets(a, n - 1, S - a[n - 1]);
14+
}
15+
else
16+
return countSubsets(a, n - 1, S);
17+
}
18+
int main() {
19+
/*
20+
Given n as the size of the array and S as the required sum.
21+
a[i] is the ith element of the array which is >0
22+
To find:
23+
The count of subsets having the sum S
24+
Similar to the subset sum problem, just instead of || use +
25+
*/
26+
ll n, S;
27+
cin >> n >> S;
28+
ll a[n];
29+
for (ll i = 0; i < n; i++)
30+
cin >> a[i];
31+
cout << countSubsets(a, n, S) << endl;
32+
33+
/*
34+
INPUT:
35+
6 10
36+
2 3 5 6 8 10
37+
OUTPUT:
38+
3
39+
*/
40+
return 0;
41+
}
42+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
#define ll long long
4+
void deleteMiddleElement(stack<ll> &s, ll n) {
5+
if (s.empty())
6+
return;
7+
ll val = s.top();
8+
s.pop();
9+
deleteMiddleElement(s, n - 1);
10+
if (n != 0) // if the element is middle element then do not add it
11+
s.push(val);
12+
}
13+
int main() {
14+
stack<ll> s;
15+
ll n; cin >> n;
16+
for (ll i = 0; i < n; i++) {
17+
ll x; cin >> x;
18+
s.push(x);
19+
}
20+
deleteMiddleElement(s, s.size() / 2);
21+
while (!s.empty()) {
22+
cout << s.top() << " ";
23+
s.pop();
24+
25+
}
26+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
#define ll long long
4+
5+
ll editDistance(string X, string Y, ll n, ll m) {
6+
/*
7+
The base conditions:
8+
If either of the strings is empty you need the corresponding lengths to
9+
make them same
10+
If the characters are same do nothing and go for a smaller input
11+
If not, we need to do something which costs 1 and them we choose the least cost amongst
12+
insert, replace and remove.
13+
Replace-> call for(n-1,m-1)
14+
Insert-> call for(n,m-1)
15+
Remove->call for(n-1,m)
16+
17+
*/
18+
if (n == 0)
19+
return m;
20+
if (m == 0)
21+
return n;
22+
if (X[n - 1] == Y[m - 1])
23+
return editDistance(X, Y, n - 1, m - 1);
24+
else
25+
return 1 + min(editDistance(X, Y, n - 1, m - 1), min(editDistance(X, Y, n - 1, m), editDistance(X, Y, n, m - 1)));
26+
}
27+
int main() {
28+
29+
/*
30+
Edit Distance.
31+
Convert String X to string Y by doing insert, replace and remove.
32+
All three are of the same cost.
33+
Find the minimum cost of conversion
34+
*/
35+
string X, Y; cin >> X >> Y;
36+
cout << editDistance(X, Y, X.size(), Y.size());
37+
38+
return 0;
39+
}
40+
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
#define ll long long
4+
5+
bool equalSumPartition(ll a[], ll n, ll S) {
6+
if (n == 0 && S == 0)
7+
return 1;
8+
if (n == 0 && S != 0)
9+
return 0;
10+
if (n != 0 && S == 0)
11+
return 1;
12+
if (a[n - 1] <= S) {
13+
return equalSumPartition(a, n - 1, S) || equalSumPartition(a, n - 1, S - a[n - 1]);
14+
}
15+
else
16+
return equalSumPartition(a, n - 1, S);
17+
}
18+
int main() {
19+
/*
20+
Given Input:
21+
n denotes the number of elements in the array
22+
a[i] is the ith element of the array
23+
S is the sum of the array
24+
To find:
25+
Return True if the array can be divided into 2 subsets of equal sum else return false
26+
27+
28+
Interpretations:
29+
Problem is similar to 0-1 knapsack.
30+
We gotta make choices.
31+
a. include it
32+
b. don't include the element.
33+
34+
*/
35+
ll n, S=0;
36+
cin >> n;
37+
ll a[n];
38+
for (ll i = 0; i < n; i++)
39+
cin >> a[i], S += a[i];
40+
if (S & 1)
41+
cout << "NO" << endl;
42+
else
43+
cout << (equalSumPartition(a, n, S / 2) ? "YES" : "NO") << endl;
44+
/*
45+
Sample Input:
46+
5
47+
2 3 5 7 8
48+
Output:
49+
NO
50+
51+
Sample Input:
52+
5
53+
5 1 1 1 2
54+
Output: YES
55+
*/
56+
57+
return 0;
58+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
#define ll long long
4+
5+
bool subsetSum(ll a[], ll n, ll S)
6+
{
7+
/*
8+
Base case:
9+
If n==0 && S==0 we can have an empty subset
10+
If n==0 && S!=0 we cannot have empty subset
11+
If n!=0 && S==0 we can choose an empty subset
12+
Think of the smallest possible input which keeps on changing in the
13+
revursive code.
14+
*/
15+
if (n == 0 && S == 0)
16+
return 1;
17+
else if (n == 0 && S != 0)
18+
return 0;
19+
else if (n != 0 && S == 0)
20+
return 1;
21+
22+
/*
23+
The choice diagram.
24+
Start from the back.
25+
We used max in the 0-1 knapsack, here we use || because if either of the two sums
26+
is possible we should get a 1. Similar to the boolean max thing(is || only).
27+
28+
*/
29+
if (a[n - 1] <= S) {
30+
return subsetSum(a, n - 1, S - a[n - 1]) || subsetSum(a, n - 1, S);
31+
}
32+
else {
33+
return subsetSum(a, n - 1, S);
34+
}
35+
}
36+
int main() {
37+
38+
/*
39+
Given Input:
40+
n denotes the number of elements in the array
41+
a[i] is the ith element of the array
42+
S is the target sum.
43+
To find:
44+
Return True if any subset with the given sum exists else return false.
45+
46+
Interpretations:
47+
Problem is similar to 0-1 knapsack.
48+
We gotta make choices.
49+
a. include it
50+
b. don't include the element.
51+
52+
*/
53+
ll n, S;
54+
cin >> n >> S;
55+
ll a[n];
56+
for (ll i = 0; i < n; i++)
57+
cin >> a[i];
58+
cout << (subsetSum(a, n, S) ? "YES" : "NO") << endl;
59+
/*
60+
Sample Input:
61+
5 1
62+
2 3 5 7 8
63+
Output:
64+
NO
65+
66+
Sample Input:
67+
5 11
68+
2 3 5 7 8
69+
Output: YES
70+
*/
71+
return 0;
72+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /