Open In App

Python Program to Print a given matrix in reverse spiral form

Improve
Improve
Like Article
Like
Save
Share
Report

Given a 2D array, print it in reverse spiral form. We have already discussed Print a given matrix in spiral form. This article discusses how to do the reverse printing. See the following examples. 
 

Input:
        1    2   3   4
        5    6   7   8
        9   10  11  12
        13  14  15  16
Output: 
10 11 7 6 5 9 13 14 15 16 12 8 4 3 2 1
Input:
        1   2   3   4  5   6
        7   8   9  10  11  12
        13  14  15 16  17  18
Output: 
11 10 9 8 7 13 14 15 16 17 18 12 6 5 4 3 2 1

 

 

Python3




# Python3 Code to Print a given 
# matrix in reverse spiral form
 
# This is a modified code of
# https:#www.geeksforgeeks.org/print-a-given-matrix-in-spiral-form/
R, C = 3, 6
 
def ReversespiralPrint(m, n, a):
 
    # Large array to initialize it
    # with elements of matrix
    b = [0 for i in range(100)]
 
    #/* k - starting row index
    #l - starting column index*/
    i, k, l = 0, 0, 0
 
    # Counter for single dimension array
    # in which elements will be stored
    z = 0
 
    # Total elements in matrix
    size = m * n
 
    while (k < m and l < n):
         
        # Variable to store value of matrix.
        val = 0
 
        # Print the first row
        # from the remaining rows
        for i in range(l, n):
             
            # printf("%d ", a[k][i])
            val = a[k][i]
            b[z] = val
            z += 1
        k += 1
 
        # Print the last column
        # from the remaining columns
        for i in range(k, m):
 
            # printf("%d ", a[i][n-1])
            val = a[i][n - 1]
            b[z] = val
            z += 1
 
        n -= 1
 
        # Print the last row
        # from the remaining rows
        if (k < m):
            for i in range(n - 1, l - 1, -1):
                 
                # printf("%d ", a[m-1][i])
                val = a[m - 1][i]
                b[z] = val
                z += 1
 
        m -= 1
 
        # Print the first column
        # from the remaining columns
        if (l < n):
            for i in range(m - 1, k - 1, -1):
                 
                # printf("%d ", a[i][l])
                val = a[i][l]
                b[z] = val
                z += 1
            l += 1
 
    for i in range(size - 1, -1, -1):
        print(b[i], end = " ")
 
# Driver Code
a = [[1, 2, 3, 4, 5, 6],
     [7, 8, 9, 10, 11, 12],
     [13, 14, 15, 16, 17, 18]]
 
ReversespiralPrint(R, C, a)
 
# This code is contributed by mohit kumar


Output: 

11 10 9 8 7 13 14 15 16 17 18 12 6 5 4 3 2 1

Time complexity: O(m*n) where m is number of rows and n is number of columns of a given matrix

Auxiliary Space: O(1) because constant space has been used

Please refer complete article on Print a given matrix in reverse spiral form for more details!

 Using Recursion:

Approach:

This approach uses recursion to traverse the matrix in reverse spiral form. We start by printing the last row from right to left, then the last column from bottom to top, then the first row from left to right, and finally the first column from top to bottom. We then recursively call the function on the remaining submatrix.

The function print_reverse_spiral takes a matrix as input and initializes variables m and n to hold the number of rows and columns in the matrix, respectively.

The function defines a helper function called print_spiral_helper which takes four parameters: row_start, row_end, col_start, and col_end. These parameters define the boundaries of the current spiral.

The helper function first checks if the current spiral is valid (i.e., if row_start is less than or equal to row_end and col_start is less than or equal to col_end). If the spiral is not valid, the function simply returns.

The helper function then prints the elements of the last row of the spiral in reverse order, followed by the elements of the first column of the spiral (excluding the last element), followed by the elements of the first row of the spiral (excluding the first element), and finally the elements of the last column of the spiral (excluding the last element).

The helper function then calls itself recursively with updated parameters to traverse the next inner spiral.

Finally, the print_reverse_spiral function calls the print_spiral_helper function with the initial boundaries of the matrix to print its elements in reverse spiral order.

Python3




def print_reverse_spiral(matrix):
    m = len(matrix)
    n = len(matrix[0])
 
    def print_spiral_helper(row_start, row_end, col_start, col_end):
        if row_start > row_end or col_start > col_end:
            return
        for j in range(col_end, col_start - 1, -1):
            print(matrix[row_end][j], end=' ')
        for i in range(row_end - 1, row_start - 1, -1):
            print(matrix[i][col_start], end=' ')
        for j in range(col_start + 1, col_end + 1):
            print(matrix[row_start][j], end=' ')
        for i in range(row_start + 1, row_end):
            print(matrix[i][col_end], end=' ')
        print_spiral_helper(row_start + 1, row_end - 1, col_start + 1, col_end - 1)
 
    print_spiral_helper(0, m - 1, 0, n - 1)
matrix = [    [1, 2, 3, 4],
    [5, 6, 7, 8],
    [9, 10, 11, 12],
    [13, 14, 15, 16]
]
print_reverse_spiral(matrix)


Output

16 15 14 13 9 5 1 2 3 4 8 12 11 10 6 7 

Time Complexity: O(mn) – where m is the number of rows and n is the number of columns in the matrix.
Auxiliary Space: O(mn) – due to the recursive call stack.



Last Updated : 07 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads