testcase 4 is wrong,help to resolve.
Array spiral print anticlockwise
@Yukta Consider this test case:
2 6
1 2 3 4 5 6
7 8 9 10 11 12
Expected Output:
1, 7, 8, 9, 10, 11, 12, 6, 5, 4, 3, 2, END
Your Output:
1, 7, 8, 9, 10, 11, 12, 6, 2, 3, 4, 5, END
Hope this helps
please suggest some way to correct it
Check it now.
#include
using namespace std;
int main() {
int m,n,value=1;
cin>>m>>n;
int arr[m][n];
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
arr[i][j]=value;
cout<<arr[i][j]<<" ";
value++;
}
cout<<endl;
}
int dir=0;
int startrow=0,endrow=m-1,startcol=0,endcol=n-1;
while(startrow<=endrow and startcol<= endcol)
{
if(dir==0)
{
for(int i=startrow;i<=endrow;i++)
{
cout<<arr[i][startcol]<<" ";
}
startcol++;
}
else if(dir==1)
{
for(int i=startcol;i<=endcol;i++)
{
cout<<arr[endrow][i]<<" ";
}
endrow--;
}
else if(dir==2)
{
for(int i=endrow;i>=startrow;iā)
{
cout<<arr[i][endcol]<<" ";
}
endcol--;
}
else if(dir==3)
{
for(int i=endcol;i>=startcol;iā)
{
cout<<arr[startrow][i]<<" ";
}
startrow++;
}
dir=(dir+1)%4;
}
return 0;
}