Welcome back, dear reader. So, in this article, we will learn a new concept related to numbers called CATALAN NUMBERS. Let us first discuss the question.
Important Links : Problem Link, Solution Video
Welcome back, dear reader. So, in this article, we will learn a new concept related to numbers called CATALAN NUMBERS. Let us first discuss the question.
Important Links : Problem Link, Solution Video
You are given that the 0th Catalan number is 1 and the first Catalan number is also 1. You have to find the nth Catalan number given that the nth Catalan number can be calculated as shown:
You may refer to the CATALAN NUMBER VIDEO to understand the question if you have any doubts regarding it. So, dear reader, try to solve this problem on your own first and then refer to the solution.
Time Complexity: O(n2) where n is the input number..
Space Complexity: O(n)
This question is straightforward. Let us start with our first step of solving a typical dynamic programming problem.
Every term comprises multiplication of the two terms. So, if we are calculating the nth Catalan number, the first number in every term starts from index 0 till the index n-1 for n terms.
For instance, if we calculate the 2nd Catalan number, the first number in the first term is C0 and the first number in the last term is C1 i.e. n-1. So, the first number of terms will be from 0 to n-1 Catalan numbers. What about the second term?
Have a look at the image below:
If we number the Catalan terms from 0 to n-1 when we are calculating the nth Catalan number, we see that the ith term in the nth Catalan number is Ci x Cn-i-1.
So, we now know both the numbers in the ith term of a Catalan number. Now, we just have to fill the dp array.
So, by using the above formula, we can easily fill the complete dp array. The dp array filled for n=5 is shown below:
We recommend you refer to the solution video to understand the above filling procedure of the array if you have any doubts about it. However, it is very simple, we just have to apply the Catalan number formula that we have studied above.
To implement the Java Code of this solution, we request you keep in mind the below two points:
So, now that we have studied everything, let us implement the Catalan number code ourselves.
java; true";
We recommend you refer to the solution video once to understand the code written above if you have any doubts regarding it. Now, let us discuss the time and space complexity of this code.
The time complexity of the above solution is O(n2) where n is the input number. Did you think that the time complexity is O(n)? Well, it is not. This is because the inner loop runs for "i" number of times and the outer loop runs for n times. So, when i=2, the inner loop runs for 2 times, and when i=3, the inner loop runs for 3 times, and so on when i=n, the inner loop runs for n times.
So, the inner loop runs n(n+1)/2 times. Why? Think about this!!! This is what makes the time complexity go O(n2).
O(n)
The space complexity is O(n) as we make an array of size n+1.
So, dear reader, we hope that you got the time and space complexity as well. We recommend you refer to the complete solution video once to clear all your doubts if any. With this, we have completed this problem.
Here are some suggestions from our side that you do not want to miss: