forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCatalanNumber.java
More file actions
31 lines (27 loc) · 768 Bytes
/
CatalanNumber.java
File metadata and controls
31 lines (27 loc) · 768 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import java.util.*;
public class CatalanNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number: ");
int num = sc.nextInt();
System.out.println();
int[] arr = new int[num + 2];
arr[0] = 1;
arr[1] = 1;
for (int i = 1; i <= num; i++) {
for (int j = i; j > 1; j--)
arr[j] = arr[j] + arr[j - 1];
arr[i + 1] = arr[i];
for (int j = i + 1; j > 1; j--)
arr[j] = arr[j] + arr[j - 1];
System.out.printf("%d ", arr[i + 1] - arr[i]);
}
}
}
/*
Sample input and output
Enter number: 7
1 2 5 14 42 132 429
time complexity: O(n^2)
space complexity: O(n)
*/