We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 28457a9 + 12acec9 commit 5fa3bcdCopy full SHA for 5fa3bcd
Nth_Catalan_Number.java
@@ -0,0 +1,27 @@
1
+import java.io.*;
2
+
3
+import java.util.*;
4
5
+//Program to calculate Nth Catalan Number using dynamic programming
6
+public class Main {
7
8
+ public static void main(String[] args) throws Exception {
9
+ Scanner scn = new Scanner(System.in);
10
11
+ int n = scn.nextInt();
12
13
+ long[] dp = new long[n + 1];
14
+ dp[0] = 1; //since 0th Catalan number is 1
15
16
+ dp[1] = 1; //since 1st Catalan number is also 1
17
18
+ for (int i = 2; i < dp.length; i++) {
19
+ for (int j = 0; j < i; j++) {
20
+ dp[i] += dp[j] * dp[i - 1 - j];
21
+ }
22
23
24
+ System.out.println(dp[n]); //nth Catalan number
25
26
27
+}
0 commit comments