Skip to main content

Spiral Matrix

Question

What is the most efficient way to traverse a given NxN matrix in a spiral pattern?

Example 1
Input: 
4

Output:
[
[ 1, 2, 3, 4],
[12, 13, 14, 5],
[11, 16, 15, 6],
[10, 9, 8, 7]
]

Solution

all//Spiral Matrix.py


def spiral_matrix(m, n, a) :
k = 0; l = 0

''' k - starting row index
m - ending row index
l - starting column index
n - ending column index
i - iterator '''


while (k < m and l < n) :

# Print the first row from
# the remaining rows
for i in range(l, n) :
print(a[k][i], end = " ")

k += 1

# Print the last column from
# the remaining columns
for i in range(k, m) :
print(a[i][n - 1], end = " ")

n -= 1

# Print the last row from
# the remaining rows
if ( k < m) :

for i in range(n - 1, (l - 1), -1) :
print(a[m - 1][i], end = " ")

m -= 1

# Print the first column from
# the remaining columns
if (l < n) :
for i in range(m - 1, k - 1, -1) :
print(a[i][l], end = " ")

l += 1