Skip to content

Commit d85da96

Browse files
authored
Merge pull request #1641 from anandv07/main
Add RightPascalTrianglePattern in java
2 parents db4af34 + 949df71 commit d85da96

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

Patterns/RightPascalTriangle.java

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
Right Pascal Triangle pattern in java
3+
Approach:
4+
1) Identify the numbers of rows for the outer for loop
5+
2) Identify the relation of column with respect to row.
6+
In this case col = size-row+1
7+
3) for each loop , print the respective element.
8+
9+
TIME COMPLEXITY: O(N^2)
10+
SPACE COMPLEXITY: O(1)
11+
Eg: For size = 5,
12+
OUTPUT:
13+
*
14+
* *
15+
* * *
16+
* * * *
17+
* * *
18+
* *
19+
*
20+
*/
21+
public class RightPascalTriangle {
22+
23+
public static void main(String[] args) {
24+
int size = 4;
25+
pattern(size);
26+
}
27+
28+
public static void pattern(int size) {
29+
for(int row=0;row< 2*size-1;row++){
30+
int totalcol = row < size?row:2*size-row-2;
31+
for(int col =0;col <=totalcol;col++){
32+
System.out.print("* ");
33+
}
34+
System.out.println();
35+
}
36+
}
37+
}
38+

0 commit comments

Comments
 (0)