Array ( [0] => [1] => questions [2] => Basic [3] => Numbers-with-Stars )
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 2*3 4*5*6 4*5*6 2*3 1
4
1 2*3 4*5*6 7*8*9*10 7*8*9*10 4*5*6 2*3 1
5
1 2* 3 4* 5* 6 7* 8* 9*10 11*12*13*14*15 11*12*13*14*15 7* 8* 9*10 4* 5* 6 2* 3 1
Login to see Discussion
Note this problem can be solved by splitting it to two sub problems
one part prints the upper part of the problem
second part prints the lower part of the problem
Step-i)assign 1 to k
Step-ii) initialize i to 1
Step-iii) initialize j to 1
Step-iv) if j is equal to 1 print k else print * and k
Step-v) increment k and j value by one
Step-vi)repeat steps iv and v till j is less than or equal to i
Step-vii)go to new line and increment i value by one
Step-viii)repeat steps iii to vii till is less than or equal to N
(Now we are done with the upper part of the pattern)
Step-ix)assign k-N to k
Step-x)initialize count to 1
Step-xi)assign N to i
Step-xii)assign 1 to j
Step-xiii)if j equal to 1 print k else print * and k
Step-xiv)increment j and k value by one
Step-xv)repeat steps xiii and xiv till j is less than or equal to i
Step-xvi)go to new line, assign k-N-i+count to K, increment count by one value and decrement i value by one
Step-xvii)repeat steps xii to xvi 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.
Login to see Solution