Array - Spiral Print 2nd Testcase is showing wrong answer

import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int m = scan.nextInt();
int[][] array = new int[n][m];
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
array[i][j]= scan.nextInt();
}
}
int top = 0;
int bottom = n-1;
int left =0;
int right = m-1;
int dir=1;
while(top<=bottom && left<=right){
if(dir==1){
for(int j =left;j<=right;j++){
System.out.print(array[top][j]+ ", “);
}
top++;
dir=2;
}
if(dir==2){
for(int row=top;row<=bottom;row++){
System.out.print(array[row][right]+”, ");

            }
            right--;
            dir=3;
        }
        if(dir==3){
            for(int col = right;col>=left;col--){
                System.out.print(array[bottom][col]+", ");
                
            }
            bottom--;
            dir=4;
        }
        if(dir==4){
            for(int row = bottom; row>=top;row--){
                System.out.print(array[row][left]+", ");
            }
            left++;
            dir=1;

        }
    }
    System.out.print("END");
}

}

Hi Vaibhav

Your code doesn’t work for the case
3 4
1 2 3 4
1 2 3 4
1 2 3 4
The output should be 1, 2, 3, 4, 4, 4, 3, 2, 1, 1, 2, 3, END

Hi @Vaibhav-Garg-1998823966894298,
As you are not responding to this thread, I am marking your doubt as Resolved for now. Re-open it if required.
Please mark your doubts as resolved in your course’s “ Ask Doubt ” section, when your doubt is resolved.