Array ( [0] => [1] => questions [2] => Basic [3] => Triangle-Shape ) Patterns | Triangle Shape | THE INQUISITIVE





Triangle Shape

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 3 2
3 4 5 4 3
Example 2:

Input
4
Output
      1
    2 3 2
  3 4 5 4 3
4 5 6 7 6 5 4
Example 3:

Input
5
Output
        1
      2 3 2
    3 4 5 4 3
  4 5 6 7 6 5 4
5 6 7 8 9 8 7 6 5

oops

Login to see Discussion




Approach


Step-i) assign 0 to count,count1 and k
Step-ii) assign 1 to i
Step-iii) assign 1 to space
Step-iv) print two spaces and increment count by one value and space by one value
Step-v)repeat step iv till space is less than or equal to N-i
Step-vi)if k is not equal to 2*i-1 go to next steps
Step-vii)if count is less than or equal to N-1 print i+k and space and increment count
Step-viii)else print i+k-s*count1 and space and increment count1 by one value
Step-ix) increment k by one value
Step-x)repeat steps vii to ix till k is not equal to 2*i-1
Step-xi)assign 0 to k,count,count1 . Increment i by one value and go to next line
Step-xii) repeat steps iii to xi till i is less than or equal to N

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