Kindly help me in developing the intuition for this problem.
// DiagonalTraversal
/#include<bits/stdc++.h>
using namespace std;
int main()
{
int m,n;
cin>>m>>n;
int arr[m][n];
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
cin>>arr[i][j];
int l=0,r=0,dir=0;
// if((m*n)<10000 and m>0 and n>0)
// {
while(l<m and r<n)
{
if(!dir)
{
while(l>0 and r<n-1)
{
cout<<arr[l][r]<<" ";
l–;
r++;
}
cout<<arr[l][r]<<" ";
if(r==n-1)
l++;
else
r++;
}
else
{
while(r>0 and l<n-1)
{
cout<<arr[l][r]<<" ";
r--;
l++;
}
cout<<arr[l][r]<<" ";
if(l==m-1)
r++;
else
l++;
}
dir=!dir;}
// }
}
I got the basic logic but this does not pass the 2nd test case