Problem in Intuition

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

check out this code

i have commented each step so that you will understand the approach

I understand the approach but I cannot figure out why is my code wrong. It works for 2/3 test cases when I submit but I could not figure out the 2nd edge test case where it can go wrong. Hope you can help me with that. Thanks

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.