# PRINT SPIRAL MATRIX

will this code work for to store spiral matrix in a vector it is giving me runtime error and i want to know my mistake help me out…

class Solution {
public:
vector v;
void spiral(vector<vector > matrix, int r, int c,int i,int j,vector<vector> visited)
{
if(i<0||j<0||i>r-1||j>c-1){
return;
}
if(visited[i][j]==1){
return;
}
v.push_back(matrix[i][j]);
visited[i][j]=1;
v.push_back(matrix[i][j]);
spiral(matrix,r,c,i-1,j,visited);
spiral(matrix,r,c,i,j+1,visited);
spiral(matrix,r,c,i+1,j,visited);
spiral(matrix,r,c,i,j-1,visited);
}

void helper(vector<vector > matrix, int r, int c){
vector<vector > visited;
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
visited[i][j]=0;
}
}
spiral(matrix,r,c,0,0,visited);
}
vector spiralOrder(vector<vector>& matrix) {
v.clear();
int r=matrix.size();
int c=matrix[0].size();
helper(matrix,r,c);
return v;
}
};

I think the reason for the runtime error is because you’re inserting the same element twice

``````v.push_back(matrix[i][j]);
visited[i][j]=1;
v.push_back(matrix[i][j]);

``````

Try to remove one push back

There is a video in youtube of printing the matrix in spiral form.
I think channel name is TECHDOSE.
Keep Coding!!

its not working @codeanktik_099