Array ( [0] => [1] => questions [2] => Patterns [3] => Right-straight-Triangle-with-stars ) Patterns | Right straight Triangle with stars | THE INQUISITIVE





Right straight Triangle with 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
    *
  * *
* * *
  * *
    *
Example 2:

Input
4
Output
      *
    * *
  * * *
* * * *
  * * *
    * *
      *
Example 3:

Input
5
Output
        *
      * *
    * * *
  * * * * 
* * * * *
  * * * *
    * * *
      * *
        *

oops

Login to see Discussion




Approach


Note:- This problem can be solved by dividing it to two subproblems
where the first part prints the upper part of the pattern
second part prints the lower part of the pattern

Step-i)initialize i to 1
Step-ii) initialize space to i
Step-iii) print two spaces (" ")
Step-iv)increment space by one value
Step-v)repeat steps iii to iv till space is less than N
Step-vi)assign 1 to k
Step-vii) print * and space (" ")
Step-viii) increment k value by one
Step-ix)repeat steps vii and viii till k is less than or equal to i
Step-x) go to new line and increment i value by one
Step-xi)repeat steps ii to x till i is less than or equal to N
(Now we are done with the upper part of the pattern)
Step-xii)initialize i to N
Step-xiii) initialize space to i
Step-xiv) print two spaces (" ")
Step-xv)increment space by one value
Step-xvi)repeat steps xiv to xv till space is less than or equal to N
Step-xvii)assign 1 to k
Step-xviii) print * and space (" ")
Step-xix) increment k value by one
Step-xx)repeat steps xviii and xix till k is less than i
Step-xxi) go to new line and decrement i value by one
Step-xxii)repeat steps xiii to xxi till i is greater than or equal to 1

Time Complexity: O(n^2)
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