Skip to main content

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

Reverse spiral form of matrix

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

  1. Initialize element as the total number of elements in the matrix (n * m).
  2. Create an array result of size element to store the reversed spiral order.
  3. Use a function findSpiral(matrix, result, x, y, n, m, element) to traverse each layer in reverse spiral order:
    • Traverse from y to m in the current row and store elements in result.
    • Traverse from x + 1 to n in the current column and store elements in result.
    • Traverse from m - 1 to y in the last row and store elements in result.
    • Traverse from n - 1 to x + 1 in the first column and store elements in result.
    • Recursively call findSpiral for the inner layer with adjusted indices.
  4. Call findSpiral(matrix, result, 0, 0, row - 1, col - 1, element).
  5. 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

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.





Comment

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