Array ( [0] => [1] => questions [2] => Basic [3] => Hallow-Rhombus )
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
* * * * * * * * * * * * *
4
* * * * * * * * * * * * * * * * * * * * *
5
* * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Login to see Discussion
Note: This problem can be solved by dividing into two parts
one part prints the upper half pattern and other part prints the lower half pattern
Step-i)initialize i to 0
Step-ii) initialize space to 1
Step-iii) print two spaces (" ")
Step-iv)increment space by one value
Step-v)repeat steps iii to iv till space is less than or equal to N-i-1
Step-vi)assign 0 to j
Step-vii)if i equal to j or i equal to N-1 or j equal to 0 or j equal to 2*i print * and space (" ")
Step-viii)else print two space (" ")
Step-ix) increment j by one value
Step-x) repeat steps vii to ix till j is less than 2*i+1
Step-xi)go to new line and increment i by one value
Step-xii)repeat ii to ix till i is less than N(Now we are done with upper part)
Step-xiii)initialize i to N-2
Step-xiv) initialize space to 1
Step-xv) print two spaces (" ")
Step-xvi)increment space by one value
Step-xvii)repeat steps xv to xvi till space is less than or equal to N-i-1
Step-xviii)assign 0 to j
Step-xix)if i equal to j or i equal to N-1 or j equal to 0 or j equal to 2*i print * and space (" ")
Step-xx)else print two space (" ")
Step-xxi) increment j by one value
Step-xxii) repeat steps xix to xxi till j is less than 2*i+1
Step-xxiii)go to new line and decrement i by one value
Step-xxiv)repeat xiv to xxiii till i is greater than 0
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