Print a given matrix in reverse spiral form
The problem involves printing a given matrix in reverse spiral order. In the reverse spiral order, you start from the outermost layer of the matrix and move inwards while traversing the elements in a spiral pattern.
Problem Statement
Given a matrix of dimensions n x m
, we need to print its elements in reverse spiral order.
Example
Let's take a matrix as an example:
1 2 3 4 5 6
22 23 24 25 26 7
21 36 37 38 27 8
20 35 2 39 28 9
19 34 1 40 29 10
18 33 32 31 30 11
17 16 15 14 13 12
The output should be: 2 1 40 39 38 37 36 ... 11 10 9 8 7 6 5 4 3 2 1
Idea to Solve
The idea to solve this problem is to perform a series of iterations along the outer layers of the matrix. In each iteration, we will traverse the elements of the current layer in a clockwise spiral manner and store them in an array. We start from the outermost layer and move inwards until we have covered all the layers.
Pseudocode
 Initialize
element
as the total number of elements in the matrix (n * m).  Create an array
result
of sizeelement
to store the reversed spiral order.  Use a function
findSpiral(matrix, result, x, y, n, m, element)
to traverse each layer in reverse spiral order: Traverse from
y
tom
in the current row and store elements inresult
.  Traverse from
x + 1
ton
in the current column and store elements inresult
.  Traverse from
m  1
toy
in the last row and store elements inresult
.  Traverse from
n  1
tox + 1
in the first column and store elements inresult
.  Recursively call
findSpiral
for the inner layer with adjusted indices.
 Traverse from
 Call
findSpiral(matrix, result, 0, 0, row  1, col  1, element)
.  Print the elements in the
result
array.
Algorithm Explanation
The algorithm starts by calculating the total number of elements (element
) in the matrix. It
initializes an array result
to store the reversed spiral order of elements.
The findSpiral
function takes the matrix, result
array, current indices (x
,
y
), and dimensions (n
, m
) of the current layer. In each step of the function,
it traverses the current layer in four directions: left to right, top to bottom, right to left, and bottom to top.
The elements encountered in each direction are stored in the result
array.
After traversing the current layer, the function recursively calls itself to process the inner layer of the matrix.
Code Solution

1) Reverse spiral traversal of matrix in java
2) Reverse spiral traversal of matrix in c++
3) Reverse spiral traversal of matrix in c#
4) Reverse spiral traversal of matrix in c
5) Reverse spiral traversal of matrix in vb.net
6) Reverse spiral traversal of matrix in php
7) Reverse spiral traversal of matrix in node js
8) Reverse spiral traversal of matrix in python
9) Reverse spiral traversal of matrix in ruby
10) Reverse spiral traversal of matrix in scala
11) Reverse spiral traversal of matrix in swift
12) Reverse spiral traversal of matrix in kotlin
13) Reverse spiral traversal of matrix in golang
Output Explanation
For the given example matrix, the reverseSpiral
function is called, which calculates the dimensions of
the matrix and initializes the element
variable. Then, it calls the findSpiral
function,
which traverses the matrix in reverse spiral order and stores the elements in the result
array.
Finally, the elements in the result
array are printed, resulting in the output:
2 1 40 39 ... 11 10 9 8 7 6 5 4 3 2 1
.
Time Complexity
The time complexity of this algorithm is O(n * m), where n is the number of rows and m is the number of columns in
the matrix. This is because each element of the matrix is visited exactly once to store it in the
result
array. The recursive calls also contribute to this linear time complexity.
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