Array ( [0] => [1] => questions [2] => Patterns [3] => Playing-with-Numbers ) Patterns | Playing with Numbers | THE INQUISITIVE





Playing with Numbers

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*
*121*
*12321*
*121*
*1*
*
Example 2:

Input
4
Output
*
*1*
*121*
*12321*
*1234321*
*12321*
*121*
*1*
*
Example 3:

Input
5
Output
*
*1*
*121*
*12321*
*1234321*
*123454321*
*1234321*
*12321*
*121*
*1*
*

oops

Login to see Discussion




Approach


Note: This problem can be solved by dividing it to two parts
one part prints the upper part of the pattern
second part prints the lower part of the pattern

Step-i)assign 0 to i
Step-ii) print *
Step-iii) assign 1 to j
Step-iv) print value of j and space (" ")
Step-v)increment j value by one
Step-vi)repeat steps iv and v till j is less than or equal to i
Step-vii)assign i-1 to k
Step-viii) print value of k and space(" ")
Step-ix) decrement k value by one
Step-x)repeat steps viii and ix till k is greater than or equal to 1
Step-xi)if i is not equal to 0 print *
Step-xii)go to new line and increment i value by one
Step-xiii)repeat steps ii to xii till i is less than or equal to N. By now we are done with the upper part of the pattern
Step-xiv)assign N-1 to i
Step-xv) print *
Step-xvi) assign 1 to j
Step-xvii) print value of j and space (" ")
Step-xviii)increment j value by one
Step-xix)repeat steps xvii and xviii till j is less than or equal to i
Step-xx)assign i-1 to k
Step-xxi) print value of k and space(" ")
Step-xxii) decrement k value by one
Step-xxiii)repeat steps xxi and xxii till k is greater than or equal to 1
Step-xxiv)if i is not equal to 0 print *
Step-xxv)go to new line and decrement i value by one
Step-xxvi)repeat steps xv to xxv till i is greater than or equal to 1
Step-xxvii)at last print a star ("*")

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