Wave print row wise - test cases failing

Hi, my code works fine when I’ve checked, and also the samp,e test case passes but the remaining test cases are failing, can you please let me know what’s wrong with the code?

Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int [][] arr = new int [n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int arrVal = sc.nextInt();
arr[i][j] = arrVal;
}
}
int count = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
System.out.print(arr[i][count]+", ");
if((i+2) % 2 == 0) {
if(count < n-1)
count ++;
} else {
if(count > 0) {
count–;
}
}
}
}
System.out.print(“END”);

Hey @patnamsainikhil There is error in your code for example : test case like
4 5
11 12 13 14
21 22 23 24
31 32 33 34
41 42 43 44
41 42 43 44
is giving ArrayOutOfBoundException.

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.

1 Like

Thank you, problem soled.