Code compiles fine but 1 test case fails

import java.util.*;
public class Main {
public static void print(int[][] arr) {
int top = 0;
int bottom = arr.length - 1;
int left = 0;
int right = arr[top].length - 1;
int dir = 1;

	while(left<=right && top<bottom) {
		if (dir == 1) {
			for (int pointer = top; pointer <= right; pointer++) {
                 System.out.print(arr[top][pointer]+", ");
			}
			dir=2;
			top++;
		}
		if(dir==2) {
			for (int pointer = top ; pointer <= bottom; pointer++) {
				System.out.print(arr[pointer][right]+", ");
			}
			dir=3;
			right--;
		}
		if(dir==3) {
			for (int pointer = right; pointer >= left; pointer--) {
				System.out.print(arr[bottom][pointer]+", ");
			}
			bottom--;
			dir=4;
		}
		if(dir==4) {
			for (int pointer = bottom ; pointer >= top ; pointer--) {
				System.out.print(arr[pointer][left]+", ");
			}
		}
		dir=1;
		left++;
	}
	System.out.println("END");
}

public static void main(String[] args) {
	Scanner sc = new Scanner(System.in);
	int M = sc.nextInt();
	int N = sc.nextInt();

	if ((M > 0 && N > 0) && (M < 10 && N < 10)) {
		int[][] arr = new int[M][N];

		for (int row = 0; row < arr.length; row++) {
			for (int col = 0; col < N; col++) {
				arr[row][col] = sc.nextInt();
			}
		}
		print(arr);
	}

}

}

im also giving different values for row and col and code is giving right output still 1 test case fails

@nigamshubham1998,
Input:
3 4
1 2 3 4
5 6 7 8
9 10 11 12

Expected Output:
1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7, END
Your output:
1, 2, 3, 4, 8, 12, 11, 10, 9, 5, END

pls help me that how can i correct my code

@nigamshubham1998,
In sprial print clockwise, we need to print elements of first row then elements of last column then elements of last row then elements of first column and so on until all the elements of the matrix are printed. In the given case first the elements of 0th row then of 3rd column then of 3rd row then of 0th column then of 1st row then of 2nd column then of 2nd row are printed.Thus, elements are printed row and column wise alternatively forming a clockwise loop.

image

Don’t hard code direction variables.

Please refer to the approach below:

        int rowmin = 0;
        int colmin = 0;
        int rowmax = arr.length - 1;
        int colmax = arr[0].length - 1;
        while (rowmin <= rowmax && colmin <= colmax) {
            for (int col = colmin; col <= colmax && rowmin <= rowmax && colmin <= colmax; col++) {
                System.out.print(arr[rowmin][col] + ", ");
            }
            rowmin++;
            for (int row = rowmin; row <= rowmax && rowmin <= rowmax && colmin <= colmax; row++) {
                System.out.print(arr[row][colmax] + ", ");
            }
                colmax--;
            for (int col = colmax; col >= colmin && rowmin <= rowmax && colmin <= colmax; col--) {
                System.out.print(arr[rowmax][col] + ", ");
            }
            rowmax--;

            for (int row = rowmax; row >= rowmin && rowmin <= rowmax && colmin <= colmax; row--) {
                System.out.print(arr[row][colmin] + ", ");
            }
            colmin++;
        }
        System.out.println("End");