Given an n × m matrix, return its elements in reversespiral 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
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.
Input: n = 4, m = 4
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.
[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>usingnamespacestd;vector<int>reverseSpiral(vector<vector<int>>&mat){intn=mat.size();intm=mat[0].size();vector<int>spiral;vector<int>res;// Store normal spiral traversal.inttop=0,bottom=n-1;intleft=0,right=m-1;while(top<=bottom&&left<=right){// Print top row.for(inti=left;i<=right;i++)spiral.push_back(mat[top][i]);top++;// Print right column.for(inti=top;i<=bottom;i++)spiral.push_back(mat[i][right]);right--;// Print bottom row.if(top<=bottom){for(inti=right;i>=left;i--)spiral.push_back(mat[bottom][i]);bottom--;}// Print left column.if(left<=right){for(inti=bottom;i>=top;i--)spiral.push_back(mat[i][left]);left++;}}// Store reverse spiral traversal separately.for(inti=spiral.size()-1;i>=0;i--)res.push_back(spiral[i]);returnres;}// Driver Codeintmain(){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(intx:ans)cout<<x<<" ";return0;}
Java
importjava.util.ArrayList;importjava.util.List;publicclassGfG{publicstaticList<Integer>reverseSpiral(int[][]mat){intn=mat.length;intm=mat[0].length;List<Integer>spiral=newArrayList<>();List<Integer>res=newArrayList<>();// Store normal spiral traversal.inttop=0,bottom=n-1;intleft=0,right=m-1;while(top<=bottom&&left<=right){// Print top row.for(inti=left;i<=right;i++)spiral.add(mat[top][i]);top++;// Print right column.for(inti=top;i<=bottom;i++)spiral.add(mat[i][right]);right--;// Print bottom row.if(top<=bottom){for(inti=right;i>=left;i--)spiral.add(mat[bottom][i]);bottom--;}// Print left column.if(left<=right){for(inti=bottom;i>=top;i--)spiral.add(mat[i][left]);left++;}}// Store reverse spiral traversal separately.for(inti=spiral.size()-1;i>=0;i--)res.add(spiral.get(i));returnres;}// Driver Codepublicstaticvoidmain(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(intx:ans)System.out.print(x+" ");}}
Python
defreverseSpiral(mat):n=len(mat)m=len(mat[0])spiral=[]res=[]# Store normal spiral traversal.top=0bottom=n-1left=0right=m-1whiletop<=bottomandleft<=right:# Print top row.foriinrange(left,right+1):spiral.append(mat[top][i])top+=1# Print right column.foriinrange(top,bottom+1):spiral.append(mat[i][right])right-=1# Print bottom row.iftop<=bottom:foriinrange(right,left-1,-1):spiral.append(mat[bottom][i])bottom-=1# Print left column.ifleft<=right:foriinrange(bottom,top-1,-1):spiral.append(mat[i][left])left+=1# Store reverse spiral traversal separately.foriinrange(len(spiral)-1,-1,-1):res.append(spiral[i])returnres# Driver Codeif__name__=="__main__":mat=[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]ans=reverseSpiral(mat)forxinans:print(x,end=" ")
C#
usingSystem;usingSystem.Collections.Generic;classGfG{publicstaticList<int>reverseSpiral(int[][]mat){intn=mat.Length;intm=mat[0].Length;List<int>spiral=newList<int>();List<int>res=newList<int>();// Store normal spiral traversal.inttop=0,bottom=n-1;intleft=0,right=m-1;while(top<=bottom&&left<=right){// Print top row.for(inti=left;i<=right;i++)spiral.Add(mat[top][i]);top++;// Print right column.for(inti=top;i<=bottom;i++)spiral.Add(mat[i][right]);right--;// Print bottom row.if(top<=bottom){for(inti=right;i>=left;i--)spiral.Add(mat[bottom][i]);bottom--;}// Print left column.if(left<=right){for(inti=bottom;i>=top;i--)spiral.Add(mat[i][left]);left++;}}// Store reverse spiral traversal separately.for(inti=spiral.Count-1;i>=0;i--)res.Add(spiral[i]);returnres;}// Driver CodepublicstaticvoidMain(){int[][]mat=newint[][]{newint[]{1,2,3,4},newint[]{5,6,7,8},newint[]{9,10,11,12},newint[]{13,14,15,16}};List<int>ans=reverseSpiral(mat);foreach(intxinans)Console.Write(x+" ");}}
JavaScript
functionreverseSpiral(mat){letn=mat.length;letm=mat[0].length;letspiral=[];letres=[];// Store normal spiral traversal.lettop=0,bottom=n-1;letleft=0,right=m-1;while(top<=bottom&&left<=right){// Print top row.for(leti=left;i<=right;i++)spiral.push(mat[top][i]);top++;// Print right column.for(leti=top;i<=bottom;i++)spiral.push(mat[i][right]);right--;// Print bottom row.if(top<=bottom){for(leti=right;i>=left;i--)spiral.push(mat[bottom][i]);bottom--;}// Print left column.if(left<=right){for(leti=bottom;i>=top;i--)spiral.push(mat[i][left]);left++;}}// Store reverse spiral traversal separately.for(leti=spiral.length-1;i>=0;i--)res.push(spiral[i]);returnres;}// Driver Codeletmat=[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]];letans=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) AuxiliarySpace: 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>usingnamespacestd;vector<int>reverseSpiral(vector<vector<int>>&mat){intn=mat.size();intm=mat[0].size();vector<int>res;inttop=0,bottom=n-1;intleft=0,right=m-1;// standard spiral traversal (clockwise)while(top<=bottom&&left<=right){for(inti=left;i<=right;i++)res.push_back(mat[top][i]);top++;for(inti=top;i<=bottom;i++)res.push_back(mat[i][right]);right--;if(top<=bottom){for(inti=right;i>=left;i--)res.push_back(mat[bottom][i]);bottom--;}if(left<=right){for(inti=bottom;i>=top;i--)res.push_back(mat[i][left]);left++;}}// reverse to get reverse spiral orderreverse(res.begin(),res.end());returnres;}// Driver Codeintmain(){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(intx:ans)cout<<x<<" ";return0;}
Java
importjava.util.ArrayList;importjava.util.Collections;importjava.util.List;publicclassGfG{publicstaticList<Integer>reverseSpiral(int[][]mat){intn=mat.length;intm=mat[0].length;List<Integer>res=newArrayList<>();inttop=0,bottom=n-1;intleft=0,right=m-1;// standard spiral traversal (clockwise)while(top<=bottom&&left<=right){for(inti=left;i<=right;i++)res.add(mat[top][i]);top++;for(inti=top;i<=bottom;i++)res.add(mat[i][right]);right--;if(top<=bottom){for(inti=right;i>=left;i--)res.add(mat[bottom][i]);bottom--;}if(left<=right){for(inti=bottom;i>=top;i--)res.add(mat[i][left]);left++;}}// reverse to get reverse spiral orderCollections.reverse(res);returnres;}// Driver Codepublicstaticvoidmain(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(intx:ans)System.out.print(x+" ");}}
Python
defreverseSpiral(mat):n=len(mat)m=len(mat[0])res=[]top=0bottom=n-1left=0right=m-1# standard spiral traversal (clockwise)whiletop<=bottomandleft<=right:foriinrange(left,right+1):res.append(mat[top][i])top+=1foriinrange(top,bottom+1):res.append(mat[i][right])right-=1iftop<=bottom:foriinrange(right,left-1,-1):res.append(mat[bottom][i])bottom-=1ifleft<=right:foriinrange(bottom,top-1,-1):res.append(mat[i][left])left+=1# reverse to get reverse spiral orderres.reverse()returnres# Driver Codeif__name__=="__main__":mat=[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]ans=reverseSpiral(mat)forxinans:print(x,end=" ")
C#
usingSystem;usingSystem.Collections.Generic;publicclassGfG{publicstaticList<int>reverseSpiral(int[,]mat){intn=mat.GetLength(0);intm=mat.GetLength(1);List<int>res=newList<int>();inttop=0,bottom=n-1;intleft=0,right=m-1;// standard spiral traversal (clockwise)while(top<=bottom&&left<=right){for(inti=left;i<=right;i++)res.Add(mat[top,i]);top++;for(inti=top;i<=bottom;i++)res.Add(mat[i,right]);right--;if(top<=bottom){for(inti=right;i>=left;i--)res.Add(mat[bottom,i]);bottom--;}if(left<=right){for(inti=bottom;i>=top;i--)res.Add(mat[i,left]);left++;}}// reverse to get reverse spiral orderres.Reverse();returnres;}// Driver CodepublicstaticvoidMain(){int[,]mat={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};List<int>ans=reverseSpiral(mat);foreach(intxinans)Console.Write(x+" ");}}
JavaScript
functionreverseSpiral(mat){letn=mat.length;letm=mat[0].length;letres=[];lettop=0,bottom=n-1;letleft=0,right=m-1;// standard spiral traversal (clockwise)while(top<=bottom&&left<=right){for(leti=left;i<=right;i++)res.push(mat[top][i]);top++;for(leti=top;i<=bottom;i++)res.push(mat[i][right]);right--;if(top<=bottom){for(leti=right;i>=left;i--)res.push(mat[bottom][i]);bottom--;}if(left<=right){for(leti=bottom;i>=top;i--)res.push(mat[i][left]);left++;}}// reverse to get reverse spiral orderres.reverse();returnres;}// Driver Codeletmat=[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]];letans=reverseSpiral(mat);for(letxofans)console.log(x+' ');