16. Nth Catalan Number
Last updated
Was this helpful?
Last updated
Was this helpful?
The problem can be found at the following link:
The Catalan numbers form a sequence characterized by the following recurrence relationship:
In this series, each term (C_i) is calculated by combining the previous terms (C_i-1, C_i-2, ...) using multiplicative relationships.
To calculate the nth Catalan number, I used dynamic programming.
I iteratively computed the Catalan numbers up to n
using a bottom-up approach.
For each i from 2 to n
, I calculated c[i]
by summing up the products of c[j]
and c[i - 1 - j]
for all valid j values.
The result is stored in c[n]
, which represents the nth Catalan number.
For instance, let's take n = 4:
So, the 4th Catalan number is 14.
Time Complexity: The time complexity of this approach is O(n^2)
, where n is the given input.
Auxiliary Space Complexity: The auxiliary space complexity is O(n)
, as I used an array c
of size n+1
to store intermediate results.
For discussions, questions, or doubts related to this solution, please visit our . We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star to the repository.