forked from architsingla13/InterviewBit-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKthPascal.java
More file actions
64 lines (52 loc) · 1.48 KB
/
KthPascal.java
File metadata and controls
64 lines (52 loc) · 1.48 KB
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
package Array;
import java.util.ArrayList;
/**
* Author - archit.s
* Date - 21/08/18
* Time - 11:30 PM
*/
public class KthPascal {
public ArrayList<Integer> getRow(int A) {
ArrayList<Integer> row1 = new ArrayList<>();
ArrayList<Integer> row2 = new ArrayList<>();
row1.add(1);
row2.add(1);
row2.add(1);
if(A == 0){
return row1;
}
else if(A == 1){
return row2;
}
else{
int count = 2;
while(count <= A){
if(count%2 == 0){
int newCount = row2.size() + 1;
for(int i = row1.size(); i< newCount; i++ ){
row1.add(1);
}
int temp = row1.size()-1;
for(int j=1; j< temp; j++){
row1.set(j, row2.get(j) + row2.get(j-1));
}
}
else{
int newCount = 1 + row1.size();
for(int i = row2.size(); i<newCount; i++ ){
row2.add(1);
}
int temp = row2.size()-1;
for(int j=1; j< temp; j++){
row2.set(j, row1.get(j) + row1.get(j-1));
}
}
count++;
}
if(count%2 == 1){
return row1;
}
return row2;
}
}
}