-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pascal'sTriangle.cpp
71 lines (57 loc) · 1.67 KB
/
Pascal'sTriangle.cpp
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
You are given an integer N. Your task is to return a 2-D ArrayList
containing the pascal’s triangle till the row N.
A Pascal's triangle is a triangular array constructed by summing adjacent elements
in preceding rows. Pascal's triangle contains the values of the binomial coefficient.
For example in the figure below.
For example, given integer N= 4 then you have to print.
1
1 1
1 2 1
1 3 3 1
Here for the third row, you will see that the second element
is the summation of the above two-row elements i.e. 2=1+1,
and similarly for row three 3 = 1+2 and 3 = 1+2.
//code
/*
Time Complexity: O(2^N)
Space complexity: O(N)
Where N denotes the number of Rows.
*/
//Recursive Approach
#include<vector>
long long int calPascal(long long int i, long long int j) {
if (j == 0 || j == i) {
return 1;
} else {
return calPascal(i - 1, j - 1) + calPascal(i - 1, j);
}
}
vector < vector < long long int >> printPascal(int n) {
vector < vector < long long int >> triangle;
for (int i = 0; i < n; i++) {
vector < long long int > temp;
for (int j = 0; j <= i; j++) {
temp.push_back(calPascal((long long int) i, (long long int) j));
}
triangle.push_back(temp);
}
return triangle;
}
//optimized approach
#include <bits/stdc++.h>
#include<vector>
vector<vector<long long int>> printPascal(int n)
{
vector<vector<long long int > > triangle;
vector<long long int > temp;
for(int i=1;i<=n;i++){
long long int rCe = 1;
temp.clear();
for(int j=1;j <= i; j++){
temp.push_back(rCe);
rCe = rCe * (i-j)/j;
}
triangle.push_back(temp);
}
return triangle;
}