File tree 1 file changed +38
-0
lines changed
1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change
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
+
You can’t perform that action at this time.
0 commit comments