Array ( [0] => [1] => questions [2] => Arrays [3] => Triangle-Shape )
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 2 3 4 5 4 3
4
1 2 3 2 3 4 5 4 3 4 5 6 7 6 5 4
5
1 2 3 2 3 4 5 4 3 4 5 6 7 6 5 4 5 6 7 8 9 8 7 6 5
Login to see Discussion
Step-i) assign 0 to count,count1 and k
Step-ii) assign 1 to i
Step-iii) assign 1 to space
Step-iv) print two spaces and increment count by one value and space by one value
Step-v)repeat step iv till space is less than or equal to N-i
Step-vi)if k is not equal to 2*i-1 go to next steps
Step-vii)if count is less than or equal to N-1 print i+k and space and increment count
Step-viii)else print i+k-s*count1 and space and increment count1 by one value
Step-ix) increment k by one value
Step-x)repeat steps vii to ix till k is not equal to 2*i-1
Step-xi)assign 0 to k,count,count1 . Increment i by one value and go to next line
Step-xii) repeat steps iii to xi till i is less than or equal to 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