code :
#include
#include
using namespace std;
int main() {
int m, n;
cin>>m>>n;
vector< vector > a(m, vector (n, 0));
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
cin>>a[i][j];
}
}
// for(int i = 0; i < m; i++)
// {
// for(int j = 0; j < n; j++)
// {
// cout<<a[i][j]<<" ";
// }
// cout<<endl;
// }
int startRow = 0, endRow = m-1;
int startCol = 0, endCol = n -1;
while(startCol <= endCol && startRow <= endRow)
{
//for first column
// if(startCol <= endCol && startRow <= endRow){
// for(int i = startRow; i <= endRow; i++)
// cout<<i<<endl;
for(int i = startRow; i <= endRow; i++)
{
cout<<a[i][startCol]<<", ";
}
startCol++;
// cout<<a[2][0]<<endl;
// cout<<"\n"<<"startCol = "<<startCol<<" endRow = "<<endRow<<" endCol = "<<endCol<<" startRow = "<<startRow<<endl;
// }
//for last row
// if(startCol <= endCol && startRow <= endRow){
for(int i = startCol; i <= endCol; i++)
cout<<a[endRow][i]<<", ";
endRow--;
// }
// cout<<"\n"<<"startCol = "<<startCol<<" endRow = "<<endRow<<" endCol = "<<endCol<<" startRow = "<<startRow<<endl;
if(startCol < endCol ){
for(int i = endRow; i >= startRow; i--){
cout<<a[i][endCol]<<", ";
// cout<<i<<endl;
}
endCol--;
}
// cout<<"\n"<<"startCol = "<<startCol<<" endRow = "<<endRow<<" endCol = "<<endCol<<" startRow = "<<startRow<<endl;
if(startRow < endRow){
for(int i = endCol; i >= startCol; i--)
cout<<a[startRow][i]<<", ";
startRow++;
}
// cout<<"\n"<<"startCol = "<<startCol<<" endRow = "<<endRow<<" endCol = "<<endCol<<" startRow = "<<startRow<<endl;
}
cout<<"END"<<endl;
return 0;
}
I am unable to think about the test case at which my code is getting wrong ouput, please suggest some test case or check if there’s any bug in it