Print a given matrix in anticlockwise spiral form
Printing a given matrix in anticlockwise spiral form is a common problem in computer programming. The goal is to traverse the elements of the matrix in an anticlockwise spiral order, starting from the top-left corner and moving towards the center.

Problem Statement
Given a matrix, the task is to print its elements in an anticlockwise spiral order. This means that the elements are traversed in an inward spiral manner, moving from the top-left corner to the center of the matrix.
Example
Consider the following 6x6 matrix:
1 2 3 4 5 6
22 23 24 25 26 7
21 36 37 38 27 8
20 35 42 39 28 9
19 34 41 40 29 10
18 33 32 31 30 11
17 16 15 14 13 12
The anticlockwise spiral traversal of the matrix is:
1 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 23 36 35 34 33 32 31 30 29 28 27 26 25 24 37 42 41 40 39 38
Idea to Solve the Problem
To solve this problem, we can iterate through the matrix in an anticlockwise spiral order by following these steps:
- Traverse the top row from
startCol
toendCol
and print each element. - Traverse the right column from
startRow + 1
toendRow
and print each element. - Traverse the bottom row from
endCol - 1
tostartCol
and print each element. - Traverse the left column from
endRow - 1
tostartRow + 1
and print each element. - If there is an inner submatrix (when
startRow + 1 <= endRow - 1
andstartCol + 1 <= endCol - 1
), make a recursive call with the inner submatrix as arguments.
Pseudocode
function anticlockwiseSpiral(matrix, startRow, startCol, endRow, endCol, element):
Traverse top row from startCol to endCol and print elements
Traverse right column from startRow + 1 to endRow and print elements
Traverse bottom row from endCol - 1 to startCol and print elements
Traverse left column from endRow - 1 to startRow + 1 and print elements
If startRow + 1 <= endRow - 1 and startCol + 1 <= endCol - 1:
Recursive call with inner submatrix
Algorithm Explanation
-
The algorithm takes a matrix and boundaries (
startRow
,startCol
,endRow
,endCol
) as inputs. -
It traverses the top row from
startCol
toendCol
and prints each element. -
It traverses the right column from
startRow + 1
toendRow
and prints each element. -
It traverses the bottom row from
endCol - 1
tostartCol
and prints each element. -
It traverses the left column from
endRow - 1
tostartRow + 1
and prints each element. -
If there is an inner submatrix (when
startRow + 1 <= endRow - 1
andstartCol + 1 <= endCol - 1
), the algorithm makes a recursive call to the same function with the inner submatrix as arguments.
Program List
-
1) Print given matrix in anticlockwise spiral form in java
2) Print given matrix in anticlockwise spiral form in c++
3) Print given matrix in anticlockwise spiral form in c
4) Print given matrix in anticlockwise spiral form in c#
5) Print given matrix in anticlockwise spiral form in vb.net
6) Print given matrix in anticlockwise spiral form in php
7) Print given matrix in anticlockwise spiral form in node js
8) Print given matrix in anticlockwise spiral form in typescript
9) Print given matrix in anticlockwise spiral form in python
10) Print given matrix in anticlockwise spiral form in ruby
11) Print given matrix in anticlockwise spiral form in scala
12) Print given matrix in anticlockwise spiral form in swift
13) Print given matrix in anticlockwise spiral form in kotlin
14) Print given matrix in anticlockwise spiral form in golang
Time Complexity
The time complexity of the anticlockwise spiral matrix traversal algorithm is O(m * n), where m is the number of rows and n is the number of columns in the matrix. This is because each element of the matrix is visited exactly once.
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment