Array ( [0] => [1] => questions [2] => Basic [3] => Playing-with-Numbers )
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* *121* *12321* *121* *1* *
4
* *1* *121* *12321* *1234321* *12321* *121* *1* *
5
* *1* *121* *12321* *1234321* *123454321* *1234321* *12321* *121* *1* *
Login to see Discussion
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.
Login to see Solution