Nth catalan number | Practice | GeeksforGeeks

Given a number N. The task is to find the Nth catalan number.
The first few Catalan numbers for N = 0, 1, 2, 3, ... are 1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, ...
Note: Positions start from 0 as shown above.
Example 1:
Input:
N = 5
Output: 42

Example 2:
Input:
N = 4
Output: 14
Your Task:
Complete findCatalan() function that takes n as an argument and returns the Nth Catalan number. The output is printed by the driver code.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Constraints:
1 <= N <= 100
+1
shekharankur44 days ago
class Solution
{
    public:
    //Function to find the nth catalan number.
    cpp_int factorial(int n){
        if(n==0)
            return 1;
        return n*factorial(n-1);
    }
    cpp_int findCatalan(int n) 
    {
        //code here
        return factorial(2*n)/(factorial(n) * factorial(n+1));
    }
};
+1
vivekjmodiya6 days ago
 
class Solution{   public:   //Function to find the nth catalan number.   cpp_int findCatalan(int n)    {       cpp_int ans = 1;       for(cpp_int i=1; i<=n; i++){           ans = (ans*(4*i - 2))/(i+1);       }       return ans;   }};
 
0
triloki352 weeks ago
cpp_int findCatalan(int n)    {      cpp_int dp[n+1]={0};      dp[0]=1;      dp[1]=1;            for(int i=2;i<=n;i++)      {          int start = 0;          int end = i-1;                    while(start<=i-1 && end>=0)          {              dp[i]+= dp[start]*dp[end];              start++;              end--;          }      }      return dp[n];   }
0
makwanajoy20032 weeks ago
cpp_int findCatalan(int n)    {       if (n==0){           return 1;       }       return (4*n - 2)*findCatalan(n-1)/(n+1);   }
0
user_u8gi2 weeks ago
class Solution{   public:   //Function to find the nth catalan number.   cpp_int findCatalan(int n)    {       //code here       cpp_int a[n+1];       a[0]=1;a[1]=1;       for(int i=2;i<n+1;i++)       {           a[i]=a[i-1]*(2*(2*i-1))/(i+1);       }       return a[n];   }};
+1
rituprasad96542 weeks ago
JAVA CODE: Dynamic Programming

class Solution
{
    //Function to find the nth catalan number.

    public static BigInteger findCatalan(int n)
    {
        //Your code here
        
        BigInteger []dp=new BigInteger[n+1];
        
        Arrays.fill(dp, BigInteger.valueOf(0));
        dp[0]=BigInteger.valueOf(1);
        dp[1]=BigInteger.valueOf(1);
        
        
        
        for(int i=2;i<=n;i++)
        {
            for(int j=0;j<i;j++)
            {   
                dp[i]=dp[i].add(dp[j].multiply(dp[i-j-1]));
            }
        }
        
        return dp[n];
    }
}
0
abhinavashish1999
This comment was deleted.
0
vikasingh3 weeks ago
Python3:-

class Solution:
    #Function to find the nth catalan number.
    def findCatalan(self,n):
        #return the nth Catalan number.
        
        if (n == 0):
           return 1
        lst = [0] * (n+1)
        lst[0] = 1
        for i in range(0, n):
            x = 0
            for j in range(0, n):
                x = x + lst[j]*lst[i-j]
            lst[i+1] = x
        return lst[n]
+1
siddhant073 weeks ago
cpp_int findCatalan(int n) 
    {
        cpp_int dp[n + 1];
        dp[0] = 1;
        dp[1] = 1;
        for(int i = 2; i <= n; i++){
            dp[i] = 0;
            for(int j = 0; j < i; j++){
                dp[i] += dp[j]*dp[i - 1 - j];
            }
        }
        return dp[n];
    }
+2
vi5hnu1 month ago
cpp_int findCatalan(int n) {       vector<cpp_int> res(n+1,0);       res[0]=1;       res[1]=1;       for(int i=2;i<n+1;i++){           for(int j=0;j<i;j++){               res[i]+=res[j]*res[i-j-1];           }       }       return res[n];   }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested 
                                    against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code.
                                    On submission, your code is tested against multiple test cases consisting of all
                                        possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as
                                        the final solution code.
You can view the solutions submitted by other users from the submission tab.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems does not guarantee the 
                                correctness of code. On submission, your code is tested against multiple test cases
                                    consisting of all possible corner cases and stress constraints.