Array ( [0] => [1] => questions [2] => Patterns [3] => Standing-Triangle-with-Numbers,-Stars )
Print the pattern as shown below for all test cases.
First line contains the value N.
Print the output as shown in below examples.
3
1 2*2 3*3*3 3*3*3 2*2 1
4
1 2*2 3*3*3 4*4*4*4 4*4*4*4 3*3*3 2*2 1
5
1 2*2 3*3*3 4*4*4*4 5*5*5*5*5 4*4*4*4 3*3*3 2*2 1
Login to see Discussion
Note: This problem can be divided into two parts
where the first part prints the upper half of the pattern
second part prints the lower half of the pattern
Step-i)initialize i to 1
Step-ii) initialize j to 1
Step-iii) if j is equal to 1 print i else print * and i
Step-iv) increment j value by one
Step-v) repeat steps iii to iv till j is less than or equal to i
Step-vi) go to next line and increment i by one value
Step-vii) repeat steps ii to vi till i is less than or equal N
(By now we are done with the upper part of the pattern)
Step-viii)assign N to i
Step-ix) assign 1 to j
Step-x)if j is equal to 1 print i or print * and i
Step-xi) increment j by one value
Step-xii)repeat steps x and xi till j is less than or equal to i
Step-xiii)go to next line and increment i by one value
Step-xiv)repeat steps ix to xiii till i is greater than or equal to 1
Time Complexity: O(n)
Space Complexity: O(1)
Note :
Let us know if you can come up with a better approach, mail us at support@theinquisitive.in Your approach will be reviewed and posted with credits to you.
Login to see Solution