-
Notifications
You must be signed in to change notification settings - Fork 5
/
GFG_NthCatalanNumber.cpp
98 lines (79 loc) · 1.99 KB
/
GFG_NthCatalanNumber.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
https://practice.geeksforgeeks.org/problems/nth-catalan-number0817/1/#
Nth catalan number
*/
// { Driver Code Starts
//Initial template for C++
#include <boost/multiprecision/cpp_int.hpp>
using boost::multiprecision::cpp_int; // https://www.geeksforgeeks.org/factorial-large-number-using-boost-multiprecision-library/
using namespace std;
// } Driver Code Ends
//User function template for C++
class Solution
{
public:
//Function to find the nth catalan number.
cpp_int findCatalan(int n)
{
/*
vector<cpp_int> fac(2*n+1, 0);
fac[0] = fac[1] = 1;
for(int i=2; i<=2*n; i++)
fac[i] = fac[i-1]*i;
cpp_int ans = fac[2*n]/(fac[n+1] * fac[n]);
return ans;
*/
// if(n<=1)
// return 1;
// cpp_int res=0;
// for(int i=0; i<n; i++)
// {
// res+= findCatalan(i)*findCatalan(n-1-i);
// }
// return res;
cpp_int cat[n+1];
/*
for(int i=0; i<=n; i++)
cat[i] = -1;
// cat[0] = cat[1] = 1;
return solve(n, cat);
*/
cat[0] = cat[1] = 1;
for(int i=2; i<=n ; i++)
{
cat[i] = 0;
for(int j=0; j<i; j++)
cat[i] += cat[j]*cat[i-j-1];
}
return cat[n];
}
cpp_int solve(int n, cpp_int *cat)
{
if(n<=1)
return cat[n]=1;
if(cat[n] != -1)
return cat[n];
cpp_int res=0;
for(int i=0; i<n; i++)
{
res+= solve(i, cat)*solve(n-1-i, cat);
}
return cat[n]=res;
}
};
// { Driver Code Starts.
int main()
{
//taking count of testcases
int t;
cin>>t;
while(t--) {
//taking nth number
int n;
cin>>n;
Solution obj;
//calling function findCatalan function
cout<< obj.findCatalan(n) <<"\n";
}
return 0;
} // } Driver Code Ends