Array ( [0] => [1] => questions [2] => Patterns [3] => Triangle-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
0 1 0 1 2 1 0 1 2
4
0 1 0 1 2 1 0 1 2 3 2 1 0 1 2 3
5
0 1 0 1 2 1 0 1 2 3 2 1 0 1 2 3 4 3 2 1 0 1 2 3 4
Login to see Discussion
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 to j to 0
Step-vii)if i equal to j print 0
Step-viii)else if j is less than i print i-j and space (" ")
Step-ix)if j is greater than i print j-i and space(" "-
Step-x) increment j value by one
Step-xi) repeat steps vii to x till j is less than 2*i+1
Step-xii) go to new line and increment i value by one
Step-xiii) repeat steps ii to xii till i is less than 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.
Login to see Solution