VOOZH about

URL: https://www.geeksforgeeks.org/dsa/program-nth-catalan-number/

⇱ Program for nth Catalan Number - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Program for nth Catalan Number

Last Updated : 2 May, 2026

Given a number n, the task is to find the nth Catalan number.  Catalan Number for n is equal to the number of expressions containing n pairs of parenthesis that are correctly matched, i.e., for each of the n(' there exist n ')' on there right and vice versa. The first few Catalan numbers for n = 0, 1, 2, 3, 4, 5… are: 1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, ...  so on.

Examples:

Input: n = 3
Output: 5
Explanation: For n = 3, there are 5 valid combinations of balanced parentheses: ((())), (()()), (())(), ()(()), ()()()

Input: n = 4
Output: 14
Explanation: For n = 4, there are 14 distinct valid combinations of balanced parentheses that can be formed.

Catalan numbers are defined as a mathematical sequence that consists of positive integers, which can be used to find the number of possibilities of various combinations.  The nth term in the sequence denoted Cn, is found in the following formula:

Catalan numbers occur in many interesting counting problems like the following.

  1. Count the number of expressions containing n pairs of parentheses that are correctly matched.
  2. Count the number of possible Binary Search Trees with n keys (See this)
  3. Count the number of full binary trees (A rooted binary tree is full if every vertex has either two children or no children) with n+1 leaves.
  4. Given a number n, return the number of ways you can draw n chords in a circle with 2 x n points such that no 2 chords intersect.

Refer to this for more applications. 

[Naive Approach] Using Recursion - O(2n) Time O(n) Space

Recursive formula:  

The nth Catalan number is calculated using the recursive formula. We recursively compute smaller Catalan values and combine them.

Algorithm:

  • If n ≤ 1, return 1 (base case).
  • Initialize result = 0.
  • Loop i from 0 to n-1, recursively calculate left = C(i) and right = C(n-i-1) and add left * right to the result.

Output
5

Time Complexity: O(2n)
Auxiliary Space: O(n)

[Better Approach] Dynamic Programming (Bottom-Up) - O(n) Time O(n) Space

The recursive solution has overlapping subproblems. So, we store previously computed Catalan numbers in an array and build the result from bottom-up using the same formula:

Algorithm:

  • Create an array catalan[] of size n+1 and Initialize catalan[0] = catalan[1] = 1.
  • Loop i from 2 to n. For each i, initialize catalan[i] = 0.
  • Run inner loop j from 0 to i-1. Add catalan[j] * catalan[i - j - 1] to catalan[i].
  • After filling the table, return catalan[n].

Output
5

Time Complexity: O(n2)
Auxiliary Space: O(n)

[Expected Approach] Binomial Coefficient – O(n) Time O(1) Space

Catalan numbers can be directly computed using the mathematical formula:

Instead of recursion or DP, we efficiently compute the binomial coefficient and divide by (n + 1).


Output
5

Time Complexity: O(n).
Auxiliary Space: O(1)

[Alternate Approach] Using Previous Catalan Number – O(n) Time O(1) Space

We already know how to calculate the nth Catalan Number using the below formula,

This formula can be further simplified to express the nth Catalan Number in the terms of (n-1)th Catalan Number,

Steps:

  • Initialize res = 1 (Catalan(0) = 1).
  • Loop from i = 2 to n. In each iteration, update result using formula: res = res * (4*i - 2) / (i + 1)
  • This builds the current Catalan number using the previous one.

Output
5

Time Complexity: O(n)
Auxiliary Space: O(1)

Comment