I have written the code but it doesnt pass the 4th test case , I am unable to think about what must be causing this.
Spiral traversal
#include
using namespace std;
int main() {
int arr[100][100];
int n,m;
cin>>n>>m;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cin>>arr[i][j];
}
}
if(n==1 && m==2){
cout<<arr[0][0]<<", END";
return 0;
}
int count= n*m;
int rs = 0;
int re = n-1;
int cs = 0;
int ce = m-1;
while(rs<=re && cs<=ce){
for(int i=rs;i<=re;i++){
cout<<arr[i][cs]<<", ";
--count;
}
cs++;
for(int i=cs;i<=ce;i++){
cout<<arr[re][i]<<", ";
--count;
}
re--;
if(cs<ce){
for(int i=re;i>=rs;i--){
cout<<arr[i][ce]<<", ";
--count;
}
ce--;
}
if(rs<re){
for(int i=ce;i>=cs;i--){
count =count -1;
cout<<arr[rs][i]<<", ";
}
rs++;
}
}
cout<<"END";
return 0;
}
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.