Skip to content

Commit

Permalink
Merge pull request #307 from Anmol55555/master
Browse files Browse the repository at this point in the history
nth Catalan Number Solution Added
  • Loading branch information
fineanmol authored Oct 2, 2021
2 parents 38f9228 + b88c3a4 commit f6de95b
Showing 1 changed file with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <bits/stdc++.h>

using namespace std;


cpp_int solve(int n, vector<cpp_int> &dp)
{
if(n == 0 || n == 1)
return 1;

if(dp[n] != -1)
return dp[n];

cpp_int ans = 0;
for(int i=0; i<=(n-1); i++)
{
ans += solve(i, dp) * solve(n-1-i, dp);
}

return dp[n] = ans;
}

cpp_int findCatalan(int n)
{
vector<cpp_int> dp(n+1, -1);

return solve(n, dp);
}

int main()
{
cpp_int n;
cin>>n;

cout<<"The "<<n<<"th Catalan Number is "<<findCatalan(n)<<endl;
}


0 comments on commit f6de95b

Please sign in to comment.