Code not accepted

import java.util.*;
public class Main {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
int[][] arr = inputarray();
rowprint(arr);
}
public static int[][] inputarray(){
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();
}
}
return arr;
}
public static void rowprint(int[][] arr) {
for(int i = 0;i<arr.length;i++) {
if(i%2==0) {
for(int j = 0;j<arr.length;j++) {
System.out.print(arr[i][j]+", β€œ);
}
}
else {
for(int j = arr.length-1;j>=0;j–) {
System.out.print(arr[i][j]+”, ");
}
}
}
System.out.print(β€œEND”);
}
}
Whats wrong in this code??

Hi @prateekad99,
It will give wrong answer for the input:
2 3
1 2 3
4 5 6

You need to add more conditions to your code. Right now you are only printing for n x n matrices.

It will give error ( Index out of bounds) for the input:
3 2
1 2
3 4
5 6

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.