#include <bits/stdc++.h>
using namespace std;
int main() {
int a[4][5] = {{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15},{16,17,18,19,20}};
int m = 4;
int n = 5;
int startRow = 0;
int startCol = 0;
int endRow = m-1;
int endCol = n-1;
while(startRow<=endRow and startCol<=endCol)
{
for(int i = startCol;i<=endCol;i++)
{
cout<<a[startRow][i]<<" ";
}
startRow++;
for(int i = startRow;i<=endRow;i++)
{
cout<<a[i][endCol]<<" ";
}
endCol--;
if(endRow>startRow)
{
for(int i = endCol;i>=startCol;i--)
{
cout<<a[endRow][i]<<" ";
}
endRow--;
}
if(endCol>startCol)
{
for(int i = endRow;i>=startRow;i--)
{
cout<<a[i][startCol]<<" ";
}
startCol++;
}
}
return 0;
}
the output should be 1 2 3 … 14 13 12 but it is coming 14 12 13