All test cases are showing run error

import java.util.*;
public class Main {
public static void main(String args[]) {
int n,m;
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
m=sc.nextInt();
int[][] arr=new int[n][m];
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
arr[i][j]=sc.nextInt();
}
}

waveprint(arr);
}
public static void waveprint(int[][] arr){
int j=0;

for(int i=0;i<arr.length;i++){
	if(i%2==0){
	for(j=0;j<arr[i].length;j++){
			System.out.print(arr[j][i]+", ");}}

else{for(j=arr[i].length-1;j>=0;j–){
System.out.print(arr[j][i]+", ");
}}}
System.out.print(β€œEND”);
}

}

Why are you printing arr[j][i]? It should be arr[i][j]. You have taken i as row and j as column in your code.

1 Like

thank you buddy i was able to solve it…

import java.util.*;
public class Main {
public static void main(String args[]) {
int m,n;
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
m=sc.nextInt();
int top=0;
int down=m-1;
int left=0;
int right=n-1;
int dir=0;
int[][] a=new int[n][m];
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
a[i][j]=sc.nextInt();
}

}

while(top<=down&&left<=right){
if(dir==0){
for(int i=left;i<=right;i++){
System.out.print(a[top][i]+", β€œ);}
top+=1;}
else if(dir==1){
for(int i=top;i<=down;i++){
System.out.print(a[i][right]+”, β€œ); }
right=right-1;
}
else if(dir==2){
for(int i=right;i>=left;i–){
System.out.print(a[down][i]+”, β€œ);}
down=down-1;
}
else if(dir==3){
for(int i=down;i>=top;i–){
System.out.print(a[i][left]+”, ");}
left=left+1;}

dir=(dir+1)%4;
}System.out.print(β€œEND”);
}
}

This code also shows the same errors.