I paste my code below, it is working for test case and also in my IDE for different cases, so I cant understand why is it failing test cases here. Please help me in resolving it:
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();
}
}
int top = 0,bottom =m-1,right = n-1,left = 0,i=0,j=0,count=0;
while(count<m*n){
for(j=left;j<right;j++){
System.out.print(arr[top][j] + ", ");
count++;
}
for(i=top;i<bottom;i++){
System.out.print(arr[i][right]+", ");
count++;
}
for(j=right;j>0;j--){
System.out.print(arr[bottom][j]+", ");
count++;
}
for(i=bottom;i>0;i--){
if(count==m*n){
System.out.print("END");
break;
}
System.out.print(arr[i][left]+", ");
count++;
}
top++;
bottom--;
right--;
left++;
}
}
}