Java Program to Print Spiral Pattern of Numbers

Given a number n as the size of the matrix, the task is to print a spiral pattern of size n.
Examples:
Input : n = 4
Output: 1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
Preview of the Spiral Pattern:

Java
public class GFG {Â
    public static void printSpiral(int size)    {Â
        // Create row and col        // to traverse rows and columns        int row = 0, col = 0;Â
        int boundary = size - 1;        int sizeLeft = size - 1;        int flag = 1;Â
        // Variable to determine the movement        // r = right, l = left, d = down, u = upper        char move = 'r';Â
        // Array for matrix        int matrix[][] = new int[size][size];Â
        for (int i = 1; i < size * size + 1; i++) {Â
            // Assign the value            matrix[row][col] = i;Â
            // switch-case to determine the next index            switch (move) {Â
            // If right, go right            case 'r':                col += 1;                break;Â
            // if left, go left            case 'l':                col -= 1;                break;Â
            // if up, go up            case 'u':                row -= 1;                break;Â
            // if down, go down            case 'd':                row += 1;                break;            }Â
            // Check if the matrix            // has reached array boundary            if (i == boundary) {Â
                // Add the left size for the next boundary                boundary += sizeLeft;Â
                // If 2 rotations has been made,                // decrease the size left by 1                if (flag != 2) {Â
                    flag = 2;                }                else {Â
                    flag = 1;                    sizeLeft -= 1;                }Â
                // switch-case to rotate the movement                switch (move) {Â
                // if right, rotate to down                case 'r':                    move = 'd';                    break;Â
                // if down, rotate to left                case 'd':                    move = 'l';                    break;Â
                // if left, rotate to up                case 'l':                    move = 'u';                    break;Â
                // if up, rotate to right                case 'u':                    move = 'r';                    break;                }            }        }Â
        // Print the matrix        for (row = 0; row < size; row++) {            for (col = 0; col < size; col++) {Â
                int n = matrix[row][col];                System.out.print((n < 10) ? (n + " ")                                          : (n + " "));            }Â
            System.out.println();        }    }Â
    // Driver Code    public static void main(String[] args)    {Â
        // Get the size of size        int size = 5;Â
        // Print the Spiral Pattern        printSpiral(size);    }} |
Output
1 2 3 4 5 16 17 18 19 6 15 24 25 20 7 14 23 22 21 8 13 12 11 10 9
Time complexity: O(n*n) where n is given size of matrix
Auxiliary space : O(n*n) for 2-d matrix



