Possible Duplicates:
Looping in a spiral
Writing a string in a spiral
Print two-dimensional array in spiral order
2d Array in Spiral Order
We have a data in the form of matrix
0 0 0 0 0
0 1 2 3 0
0 4 5 6 0
0 7 8 9 0
0 0 0 0 0
which is stored in a 1d array in this fashion
[0 0 0 0 0 0 1 2 3 0 0 4 5 6 0 0 7 8 9 0 0 0 0 0 0]
This is a zero padded 3x3 array transformed into 5x5. We know the start index and end index.
As we see, we can perform 25 operations and print all values, but instead if we go in spiral order we should ideally do this in only 9 operations.
Does anyone know how to do this?
We know the number of rows and number of columns. Here it would be rows=5 cols=5.
Hence start index would be rows+1 and end index would be rows*cols-6
I'm visualizing it as a spiral order traversal.