Spirally traversing a matrix | Practice | GeeksforGeeks

Given a matrix of size r*c. Traverse the matrix in spiral form.
Example 1:
Input:
r = 4, c = 4
matrix[][] = {{1, 2, 3, 4},
           {5, 6, 7, 8},
           {9, 10, 11, 12},
           {13, 14, 15,16}}
Output: 
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
Explanation:

Example 2:
Input:
r = 3, c = 4  
matrix[][] = {{1, 2, 3, 4},
           {5, 6, 7, 8},
           {9, 10, 11, 12}}
Output: 
1 2 3 4 8 12 11 10 9 5 6 7
Explanation:
Applying same technique as shown above, 
output for the 2nd testcase will be 
1 2 3 4 8 12 11 10 9 5 6 7.


Your Task:
You dont need to read input or print anything. Complete the function spirallyTraverse() that takes matrix, r and c as input parameters and returns a list of integers denoting the spiral traversal of matrix. 

Expected Time Complexity: O(r*c)
Expected Auxiliary Space: O(r*c), for returning the answer only.

Constraints:
1 <= r, c <= 100
0 <= matrixi <= 100
0
khan17ahmad3 days ago
vector<int> spirallyTraverse(vector<vector<int> > matrix, int r, int c) 
    {
        int n=r;
        int m=c;
        int left=0,right=m-1,bottom=n-1,top=0;
        int direction=1;
        vector<int> v;
        while(left<=right && top<=bottom)
        {
            if(direction==1)
            {
                for(int i=left;i<=right;i++) v.push_back(matrix[top][i]);
                direction=2;
                top++;
            }
            
            else if(direction==2)
            {
                for(int i=top;i<=bottom;i++) v.push_back(matrix[i][right]);
                direction=3;
                right--;
            }
            
            else if(direction==3)
            {
                for(int i=right;i>=left;i--) v.push_back(matrix[bottom][i]);
                direction=4;
                bottom--;
            }
            
            else if(direction==4)
            {
                for(int i=bottom;i>=top;i--) v.push_back(matrix[i][left]);
                direction=1;
                left++;
            }
        }
        return v;
    }
0
kkumarakash70114 days ago
 vector<int> spirallyTraverse(vector<vector<int> > mat, int r, int c)    {       // code here        vector<int> v;       int top=0;       int bottom=r-1;       int left=0;       int right=c-1;              while(top<=bottom && left<=right)       {           for(int i=left;i<=right;i++)           v.push_back(mat[top][i]);           top++;                      for(int i=top;i<=bottom;i++)           v.push_back(mat[i][right]);           right--;                      if(top<=bottom)           {               for(int i=right;i>=left;i--)               v.push_back(mat[bottom][i]);               bottom--;           }           if(left<=right)           {               for(int i=bottom;i>=top;i--)               v.push_back(mat[i][left]);               left++;           }       }       return v;
}
0
jainkunal20034 days ago
vector<int> spirallyTraverse(vector<vector<int> > arr, int r, int c) 
    {
        vector<int> ans;
        
        int sri=0;
        int eri=r-1;
        
        int sci=0;
        int eci=c-1;
        int count=0;
        
        int tot=r*c;
        
        while(count<tot){
            
            for(int i=sci;i<=eci&&count<tot;i++){
                ans.push_back(arr[sri][i]);
                count++;
            }
            
            sri++;
            
            for(int i=sri;i<=eri&&count<tot;i++){
                ans.push_back(arr[i][eci]);
                count++;
            }
            eci--;
            
            for(int i=eci;i>=sci&&count<tot;i--){
                ans.push_back(arr[eri][i]);
                count++;
            }
            
            eri--;
            
            for(int i=eri;i>=sri&&count<tot;i--){
                ans.push_back(arr[i][sci]);
                count++;
            }
            
            sci++;
        }
        
        return ans;
    }
0
mondaltarakes1 week ago
vector<int> spirallyTraverse(vector<vector<int> matrix, int r, int c) 
    {
       vector<int>v;
       int coll=0,rowu=0,colr=c-1,rowd=r-1;
       while(coll<=colr && rowu<=rowd){
           //left to right
          for(int i=coll;i<=colr;i++)
          v.push_back(matrix[rowu][i]);
          
          //top to bottom
          for(int i=rowu+1;i<=rowd;i++)
          v.push_back(matrix[i][colr]);
          
          //right to left if novisited row exist
          if(rowu!=rowd)
          for(int i=colr-1;i>=coll;i--)
          v.push_back(matrix[rowd][i]);
          
          //bottom to top if nonvisited column exist
          if(coll!=colr)
          for(int i=rowd-1;i>rowu;i--)
          v.push_back(matrix[i][coll]);
          
          coll++;
          colr--;
          rowu++;
          rowd--;
       }
       return v;
    }
0
mannukhurana103971 week ago
java
 
static ArrayList<Integer> spirallyTraverse(int arr[][], int r, int c)
    {
        ArrayList<Integer> lst = new ArrayList<Integer>();
        
        
        int start_row = 0; int end_row = r-1;
        int start_col = 0; int end_col = c-1;
        
        while(start_row<=end_row && start_col<=end_col){
            
            for(int i=start_col;i<=end_col;i++){
                lst.add(arr[start_row][i]);
            } start_row++;
            
            for(int i=start_row;i<=end_row;i++){
                lst.add(arr[i][end_col]);
            } end_col--;
            
            if(start_row<=end_row){
                for(int i=end_col;i>=start_col;i--){
                    lst.add(arr[end_row][i]);
                } end_row--;
            }
            if(start_col<=end_col){
                for(int i=end_row;i>=start_row;i--){
                    lst.add(arr[i][start_col]);
                } start_col++;
            }
            
        }
        return lst;
        
    }
+1
anishmeena0012 weeks ago
vector<int> spirallyTraverse(vector<vector<int> > matrix, int r, int c)    {       // code here       // code is written by aneesh meena       int i,k=0,l=0;       vector<int> ans;       while(k<r && l<c)                {            for(int i=l ;i<c;i++)            {                ans.push_back(matrix[k][i]);            }            k+=1;                    for(int i=k;i<r;i++)            {                ans.push_back(matrix[i][c-1]);            }            c-=1;            if(k<r){            for(int i=c-1;i>=l;i--)            {                ans.push_back(matrix[r-1][i]);            }            r-=1;}            if(l<c){            for(int i=r-1;i>=k;i--)            {                ans.push_back(matrix[i][l]);            }            l+=1;}        }        return ans;   }
0
hashshamkhan200362 weeks ago
vector<int> spirallyTraverse(vector<vector<int> > matrix, int r, int c)    {       // code here        vector<int> ans;       int row = matrix.size();       int col = matrix[0].size();              //index       int starting_row=0;       int starting_col=0;              int ending_row=row-1;       int ending_col=col-1;              int count=0;       int total=col*row;       while(count<total){                      //print starting row           for(int index=starting_col;count<total && index<=ending_col;index++){               ans.push_back(matrix[starting_row][index]);               count++;           }           starting_row++;                      //print ending col            for(int index=starting_row;count<total && index<=ending_row;index++){               ans.push_back(matrix[index][ending_col]);               count++;           }           ending_col--;                      //print ending row            for(int index=ending_col;count<total && index>=starting_col;index--){               ans.push_back(matrix[ending_row][index]);               count++;           }           ending_row--;                      //print starting col            for(int index=ending_row;count<total && index>=starting_row;index--){               ans.push_back(matrix[index][starting_col]);               count++;           }           starting_col++;       }       return ans;   }
0
kshitij14102 weeks ago
vector<int> spirallyTraverse(vector<vector<int> > matrix, int r, int c)    {       vector<int>ans;       int top=0;       int left=0;       int right=c-1;       int bottom=r-1;              while(top<=bottom and left <=right)       {           for(int i=left;i<=right;i++)           {               ans.push_back(matrix[top][i]);           }           top++;                      for(int i=top;i<=bottom;i++)           {                ans.push_back(matrix[i][right]);           }                      right--;                      if(top<=bottom)           {               for(int i=right;i>=left;i--)               {                  ans.push_back(matrix[bottom][i]);                }               bottom--;           }                      if(left<=right)           {               for(int i=bottom ;i>=top;i--)               {                   ans.push_back(matrix[i][left]);                }               left++;           }                  }              return ans;   }
0
vatsalsamart452 weeks ago
Please Help me figure out why my code is giving some garbage values
 
 
int* spirallyTraverse(int r, int c, int matrix[][c]){      int dir = 0;   int a[r*c];   int k=0;   int top = 0;   int down = r - 1;   int right = c - 1;   int left = 0;   int i;   while (top <= down && left <= right)   {       // if (dir == 0)       // {           for (i = left; i <= right; i++)           {               // cout << matrix[top][i] << " ";               a[k++]= matrix[top][i];           }           top++;       // }       // else if (dir == 1)       // {           for (i = top; i <= down; i++)           {               // cout << matrix[i][right] << " ";                a[k++]=matrix[i][right];           }           right--;       // }       // else if (dir == 2)       // {           for (i = right; i >= left; i--)           {               // cout << matrix[down][i] << " ";               a[k++]=matrix[down][i];           }           down--;       // }       // else if (dir == 3)       // {           for (i = down; i >= top; i--)           {               // cout << matrix[i][left] << " ";               a[k++]=matrix[i][left];           }           left++;       // }       // dir=(dir+1)%4;   }   int *p=a;   return p;}
0
shinu832 weeks ago
   vector<int> ans;           // int row=matrix.size();           // int col=matrix[0].size();           int startingrow=0;           int startingcol=0;           int endingrow=r-1;           int endingcol=c-1;           int count=0;           int total=r*c;           while(count<total){           for(int i=startingcol;count<total&&i<=endingcol;i++){                   ans.push_back(matrix[startingrow][i]);                   count++;           }           startingrow++;           for(int i=startingrow;count<total&&i<=endingrow;i++){                   ans.push_back(matrix[i][endingcol]);                   count++;           }           endingcol--;           for(int i=endingcol;count<total&&i>=startingcol;i--){                   ans.push_back(matrix[endingrow][i]);                   count++;           }           endingrow--;           for(int i=endingrow;count<total&&i>=startingrow;i--){                   ans.push_back(matrix[i][startingcol]);                   count++;           }           startingcol++;           }           return ans;
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.