Column wise matrix not submitting

why my code is not submitting

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int M=sc.nextInt();
int N=sc.nextInt();

int[][] arr=new int[M][N];
for(int i=0;i<M;i++)
{
	for(int j=0;j<N;j++)
	{
		arr[i][j]=sc.nextInt();
	}
}


for(int i=0;i<M;i++)
{
	if(i%2==0)
	{
		for(int j=0;j<N;j++)
		{
			System.out.print(arr[j][i]+", ");
		}
	}
	else
	{
		for(int j=N-1;j>=0;j--)
		{
			System.out.print(arr[j][i]+", ");
		}
	}
}
System.out.print("END");

}
}

@Avnish-Singh-2724104534303277,

https://ide.codingblocks.com/s/251268 corrected code buddy. There was an error in your input format. There were 2 variables for input, you took only one.

but sir i have taken two inputs M & N bt my code was nt working.

@Avnish-Singh-2724104534303277,

https://ide.codingblocks.com/s/251289 corrected code.
You had interchanged M and N in your approach while printing the elements.
That way you would have got a outofbounds error for the input:

2 3
1 2 3
4 5 6

Also, I took the code from your most recent submission before checking it. In that you had written int N=M. Hence the misjudgment.

Anyway, I hope this helps. :smile:

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.