Array ( [0] => [1] => questions [2] => Popular-Algorithms [3] => Standing-Triangle-with-Numbers,-Stars ) Patterns | Standing Triangle with Numbers, Stars | THE INQUISITIVE





Standing Triangle with Numbers, Stars

LEVEL:Beginner

Description

Print the pattern as shown below for all test cases.

Input Format

First line contains the value N.

Output Format

Print the output as shown in below examples.


Example 1:

Input
3
Output
1
2*2
3*3*3
3*3*3
2*2
1
Example 2:

Input
4
Output
1
2*2
3*3*3
4*4*4*4
4*4*4*4
3*3*3
2*2
1
Example 3:

Input
5
Output
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

oops

Login to see Discussion




Approach


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.

oops

Login to see Solution