We have a n*n matrix for example we take n=4 and the matrix is given below.
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
We have to traverse it in the sequence:
1, 2, 3, 4, 8, 12, 16, 15, 14, 13, 9, 5, 6, 7, 11, 10
How can I do this?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
“Traversing” in this case probably means accessing and printing each entry.You want to go through it in a clockwise spiral, starting from the top.
Here is an English-sentence-style description of what you need to do:
If you can find a way to go to the TOP-LEFT element of a sub-matrix, read off the entries in the top row left-to-right, read off the entries in the right column top-to-bottom, entries in the bottom row right-to-left, and then the left column bottom-to-top, you have one iteration. You can take the remaining sub-matrix and continue until you have nothing left.
Further hints:
From the cell M[x][y],