Print a given matrix in reverse spiral form

Last Updated : 28 May, 2026

Given an n × m matrix, return its elements in reverse spiral order. 

Note: Reverse spiral order starts from the center of the matrix (or the closest valid center for even dimensions) and moves outward in a spiral.

Examples:

Input: n = 3, m = 3

blobid0_1779802052

Output: [5, 6, 3, 2, 1, 4, 7, 8, 9]
Explanation: Spiral form of the matrix in reverse order starts from the centre and goes outward.

blobid0_1779806437


Input: n = 4, m = 4

blobid2_1779802154

Output: [10, 11, 7, 6, 5, 9, 13, 14, 15, 16, 12, 8, 4, 3, 2, 1]
Explanation: Spiral form of the matrix in reverse order starts from the centre and goes outward.

blobid4_1779802234
Try It Yourself
redirect icon

[Naive Approach] Generate Spiral Traversal and Reverse It - O(n * m) Time O(n * m) Space

The idea is to first traverse the matrix in normal spiral order and store all elements in a vector. Since reverse spiral order is exactly the reverse of normal spiral traversal, reverse the stored vector and return it as the answer.

C++
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

vector<int> reverseSpiral(vector<vector<int>> &mat)
{
    int n = mat.size();
    int m = mat[0].size();

    vector<int> spiral;
    vector<int> res;

    // Store normal spiral traversal.
    int top = 0, bottom = n - 1;
    int left = 0, right = m - 1;

    while (top <= bottom && left <= right)
    {

        // Print top row.
        for (int i = left; i <= right; i++)
            spiral.push_back(mat[top][i]);
        top++;

        // Print right column.
        for (int i = top; i <= bottom; i++)
            spiral.push_back(mat[i][right]);
        right--;

        // Print bottom row.
        if (top <= bottom)
        {
            for (int i = right; i >= left; i--)
                spiral.push_back(mat[bottom][i]);
            bottom--;
        }

        // Print left column.
        if (left <= right)
        {
            for (int i = bottom; i >= top; i--)
                spiral.push_back(mat[i][left]);
            left++;
        }
    }

    // Store reverse spiral traversal separately.
    for (int i = spiral.size() - 1; i >= 0; i--)
        res.push_back(spiral[i]);

    return res;
}

// Driver Code
int main()
{

    vector<vector<int>> mat = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}};

    vector<int> ans = reverseSpiral(mat);

    for (int x : ans)
        cout << x << " ";

    return 0;
}
Java
import java.util.ArrayList;
import java.util.List;

public class GfG {
    public static List<Integer> reverseSpiral(int[][] mat)
    {
        int n = mat.length;
        int m = mat[0].length;

        List<Integer> spiral = new ArrayList<>();
        List<Integer> res = new ArrayList<>();

        // Store normal spiral traversal.
        int top = 0, bottom = n - 1;
        int left = 0, right = m - 1;

        while (top <= bottom && left <= right) {

            // Print top row.
            for (int i = left; i <= right; i++)
                spiral.add(mat[top][i]);
            top++;

            // Print right column.
            for (int i = top; i <= bottom; i++)
                spiral.add(mat[i][right]);
            right--;

            // Print bottom row.
            if (top <= bottom) {
                for (int i = right; i >= left; i--)
                    spiral.add(mat[bottom][i]);
                bottom--;
            }

            // Print left column.
            if (left <= right) {
                for (int i = bottom; i >= top; i--)
                    spiral.add(mat[i][left]);
                left++;
            }
        }

        // Store reverse spiral traversal separately.
        for (int i = spiral.size() - 1; i >= 0; i--)
            res.add(spiral.get(i));

        return res;
    }

    // Driver Code
    public static void main(String[] args)
    {
        int[][] mat = { { 1, 2, 3, 4 },
                        { 5, 6, 7, 8 },
                        { 9, 10, 11, 12 },
                        { 13, 14, 15, 16 } };

        List<Integer> ans = reverseSpiral(mat);

        for (int x : ans)
            System.out.print(x + " ");
    }
}
Python
def reverseSpiral(mat):
    n = len(mat)
    m = len(mat[0])

    spiral = []
    res = []

    # Store normal spiral traversal.
    top = 0
    bottom = n - 1
    left = 0
    right = m - 1

    while top <= bottom and left <= right:

        # Print top row.
        for i in range(left, right + 1):
            spiral.append(mat[top][i])
        top += 1

        # Print right column.
        for i in range(top, bottom + 1):
            spiral.append(mat[i][right])
        right -= 1

        # Print bottom row.
        if top <= bottom:
            for i in range(right, left - 1, -1):
                spiral.append(mat[bottom][i])
            bottom -= 1

        # Print left column.
        if left <= right:
            for i in range(bottom, top - 1, -1):
                spiral.append(mat[i][left])
            left += 1

    # Store reverse spiral traversal separately.
    for i in range(len(spiral) - 1, -1, -1):
        res.append(spiral[i])

    return res


# Driver Code
if __name__ == "__main__":

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

    ans = reverseSpiral(mat)

    for x in ans:
        print(x, end=" ")
C#
using System;
using System.Collections.Generic;

class GfG {
    public static List<int> reverseSpiral(int[][] mat)
    {
        int n = mat.Length;
        int m = mat[0].Length;

        List<int> spiral = new List<int>();
        List<int> res = new List<int>();

        // Store normal spiral traversal.
        int top = 0, bottom = n - 1;
        int left = 0, right = m - 1;

        while (top <= bottom && left <= right) {
            // Print top row.
            for (int i = left; i <= right; i++)
                spiral.Add(mat[top][i]);
            top++;

            // Print right column.
            for (int i = top; i <= bottom; i++)
                spiral.Add(mat[i][right]);
            right--;

            // Print bottom row.
            if (top <= bottom) {
                for (int i = right; i >= left; i--)
                    spiral.Add(mat[bottom][i]);
                bottom--;
            }

            // Print left column.
            if (left <= right) {
                for (int i = bottom; i >= top; i--)
                    spiral.Add(mat[i][left]);
                left++;
            }
        }

        // Store reverse spiral traversal separately.
        for (int i = spiral.Count - 1; i >= 0; i--)
            res.Add(spiral[i]);

        return res;
    }

    // Driver Code
    public static void Main()
    {
        int[][] mat
            = new int[][] { new int[] { 1, 2, 3, 4 },
                            new int[] { 5, 6, 7, 8 },
                            new int[] { 9, 10, 11, 12 },
                            new int[] { 13, 14, 15, 16 } };

        List<int> ans = reverseSpiral(mat);

        foreach(int x in ans) Console.Write(x + " ");
    }
}
JavaScript
function reverseSpiral(mat)
{
    let n = mat.length;
    let m = mat[0].length;

    let spiral = [];
    let res = [];

    // Store normal spiral traversal.
    let top = 0, bottom = n - 1;
    let left = 0, right = m - 1;

    while (top <= bottom && left <= right) {

        // Print top row.
        for (let i = left; i <= right; i++)
            spiral.push(mat[top][i]);
        top++;

        // Print right column.
        for (let i = top; i <= bottom; i++)
            spiral.push(mat[i][right]);
        right--;

        // Print bottom row.
        if (top <= bottom) {
            for (let i = right; i >= left; i--)
                spiral.push(mat[bottom][i]);
            bottom--;
        }

        // Print left column.
        if (left <= right) {
            for (let i = bottom; i >= top; i--)
                spiral.push(mat[i][left]);
            left++;
        }
    }

    // Store reverse spiral traversal separately.
    for (let i = spiral.length - 1; i >= 0; i--)
        res.push(spiral[i]);

    return res;
}

// Driver Code
let mat = [
    [ 1, 2, 3, 4 ], [ 5, 6, 7, 8 ], [ 9, 10, 11, 12 ],
    [ 13, 14, 15, 16 ]
];
let ans = reverseSpiral(mat);
ans.forEach(x => console.log(x + " "));

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

Time Complexity: O(n * m)
Auxiliary Space: O(n * m)

[Expected Approach] Boundary Traversal with Final Reversal - O(n * m) Time O(1) Space

The idea is to use four boundaries (top, bottom, left, right) to traverse the matrix layer by layer in spiral order. Store the traversal in a vector and reverse the vector at the end to get the reverse spiral order starting from the center and moving outward.

C++
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

vector<int> reverseSpiral(vector<vector<int>> &mat)
{
    int n = mat.size();
    int m = mat[0].size();

    vector<int> res;

    int top = 0, bottom = n - 1;
    int left = 0, right = m - 1;

    // standard spiral traversal (clockwise)
    while (top <= bottom && left <= right)
    {

        for (int i = left; i <= right; i++)
            res.push_back(mat[top][i]);
        top++;

        for (int i = top; i <= bottom; i++)
            res.push_back(mat[i][right]);
        right--;

        if (top <= bottom)
        {
            for (int i = right; i >= left; i--)
                res.push_back(mat[bottom][i]);
            bottom--;
        }

        if (left <= right)
        {
            for (int i = bottom; i >= top; i--)
                res.push_back(mat[i][left]);
            left++;
        }
    }

    // reverse to get reverse spiral order
    reverse(res.begin(), res.end());

    return res;
}

// Driver Code
int main()
{

    vector<vector<int>> mat = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}};

    vector<int> ans = reverseSpiral(mat);

    for (int x : ans)
        cout << x << " ";

    return 0;
}
Java
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class GfG {
    public static List<Integer> reverseSpiral(int[][] mat)
    {
        int n = mat.length;
        int m = mat[0].length;

        List<Integer> res = new ArrayList<>();

        int top = 0, bottom = n - 1;
        int left = 0, right = m - 1;

        // standard spiral traversal (clockwise)
        while (top <= bottom && left <= right) {

            for (int i = left; i <= right; i++)
                res.add(mat[top][i]);
            top++;

            for (int i = top; i <= bottom; i++)
                res.add(mat[i][right]);
            right--;

            if (top <= bottom) {
                for (int i = right; i >= left; i--)
                    res.add(mat[bottom][i]);
                bottom--;
            }

            if (left <= right) {
                for (int i = bottom; i >= top; i--)
                    res.add(mat[i][left]);
                left++;
            }
        }

        // reverse to get reverse spiral order
        Collections.reverse(res);

        return res;
    }

    // Driver Code
    public static void main(String[] args)
    {
        int[][] mat = { { 1, 2, 3, 4 },
                        { 5, 6, 7, 8 },
                        { 9, 10, 11, 12 },
                        { 13, 14, 15, 16 } };

        List<Integer> ans = reverseSpiral(mat);

        for (int x : ans)
            System.out.print(x + " ");
    }
}
Python
def reverseSpiral(mat):
    n = len(mat)
    m = len(mat[0])

    res = []

    top = 0
    bottom = n - 1
    left = 0
    right = m - 1

    # standard spiral traversal (clockwise)
    while top <= bottom and left <= right:
        for i in range(left, right + 1):
            res.append(mat[top][i])
        top += 1

        for i in range(top, bottom + 1):
            res.append(mat[i][right])
        right -= 1

        if top <= bottom:
            for i in range(right, left - 1, -1):
                res.append(mat[bottom][i])
            bottom -= 1

        if left <= right:
            for i in range(bottom, top - 1, -1):
                res.append(mat[i][left])
            left += 1

    # reverse to get reverse spiral order
    res.reverse()

    return res


# Driver Code
if __name__ == "__main__":

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

    ans = reverseSpiral(mat)

    for x in ans:
        print(x, end=" ")
C#
using System;
using System.Collections.Generic;

public class GfG {
    public static List<int> reverseSpiral(int[, ] mat)
    {
        int n = mat.GetLength(0);
        int m = mat.GetLength(1);

        List<int> res = new List<int>();

        int top = 0, bottom = n - 1;
        int left = 0, right = m - 1;

        // standard spiral traversal (clockwise)
        while (top <= bottom && left <= right) {
            for (int i = left; i <= right; i++)
                res.Add(mat[top, i]);
            top++;

            for (int i = top; i <= bottom; i++)
                res.Add(mat[i, right]);
            right--;

            if (top <= bottom) {
                for (int i = right; i >= left; i--)
                    res.Add(mat[bottom, i]);
                bottom--;
            }

            if (left <= right) {
                for (int i = bottom; i >= top; i--)
                    res.Add(mat[i, left]);
                left++;
            }
        }

        // reverse to get reverse spiral order
        res.Reverse();

        return res;
    }

    // Driver Code
    public static void Main()
    {
        int[, ] mat = { { 1, 2, 3, 4 },
                        { 5, 6, 7, 8 },
                        { 9, 10, 11, 12 },
                        { 13, 14, 15, 16 } };

        List<int> ans = reverseSpiral(mat);

        foreach(int x in ans) Console.Write(x + " ");
    }
}
JavaScript
function reverseSpiral(mat) {
    let n = mat.length;
    let m = mat[0].length;

    let res = [];

    let top = 0, bottom = n - 1;
    let left = 0, right = m - 1;

    // standard spiral traversal (clockwise)
    while (top <= bottom && left <= right) {
        for (let i = left; i <= right; i++)
            res.push(mat[top][i]);
        top++;

        for (let i = top; i <= bottom; i++)
            res.push(mat[i][right]);
        right--;

        if (top <= bottom) {
            for (let i = right; i >= left; i--)
                res.push(mat[bottom][i]);
            bottom--;
        }

        if (left <= right) {
            for (let i = bottom; i >= top; i--)
                res.push(mat[i][left]);
            left++;
        }
    }

    // reverse to get reverse spiral order
    res.reverse();

    return res;
}

// Driver Code
let mat = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]];
let ans = reverseSpiral(mat);
for (let x of ans)
    console.log(x + ' ');

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

Time Complexity: O(n * m)
Auxiliary Space: O(1)

Comment