Here i am getting correct output but my testcase is wrong

import java.util.;
public class Main {
public static void main(String[] args) {
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<arr.length;i++) {
for(int j=0;j<arr.length;j++) {
arr[i][j]=sc.nextInt();
}
}
spiralprint(arr);
sc.close();
}
public static void spiralprint(int [][]arr) {
int top=0,bottom=arr.length-1,left=0,right=arr[top].length-1,dir=1,count=(bottom+1)
(right+1);
while(left<=right && top<=bottom) {
if(count>0) {
if(dir==1) {
for(int i=left;i<=right;i++) {
System.out.print(arr[top][i]+", “);
count–;
}
dir=2;
top++;
}
}
if(count>0) {
if(dir==2) {
for(int i=top;i<=bottom;i++) {
System.out.print(arr[i][bottom]+”, “);
count–;
}
dir=3;
right–;
}
}
if(count>0) {
if(dir==3) {
for(int i=right;i>=left;i–) {
System.out.print(arr[bottom][i]+”, “);
count–;
}
dir=4;
bottom–;
}
}
if(count>0) {
if(dir==4) {
for(int i=bottom;i>=top;i–) {
System.out.print(arr[i][left]+”, ");
count–;
}
dir=1;
left++;
}
}
}
System.out.println(“END”);
}
}

Hey @ketushubham098,

You had an error while taking the input, it should be:

Instead of:

Also, you had an error in this line:

Apart from that the code looks good.
I have corrected your code, and you can refer to the solution here.

Hope it helps :slight_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.