Program to print Spiral Pattern
Given a number n as the size of the matrix, the task is to print a spiral pattern in 2D array of size n.
Examples:
Input: n = 5 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
Preview of the Spiral Pattern:
Approach:
- Create a 2D array of size n
- Store the boundary of the array in boundary variable. Initially it will be n-1 and thereafter it changes after every rotation.
- Store the size left for the spiral printing in variable sizeLeft. Initially it will be n-1 and thereafter it will decrease by 1 after every 2 rotations.
- Create a flag to determine 2 rotations, as after every 2 rotations, the sizeLeft will decrease.
- Create a char variable move to store the current movement of the spiral pattern. It can have ‘r’ for right, ‘l’ for left, ‘d’ for down, and ‘u’ for up.
- Repeat the below steps till ‘i’ is in range [1, n^2]:
- Assign the value of i to the spiral pattern.
- Determine the next movement of the pattern.
- Check for pattern to reach boundary. If reached, modify the sizes and rotate the spiral pattern.
- Print the Spiral Pattern stored in the 2D array.
Below is Java implementation of the above approach:
C++
#include <iostream> using namespace std; 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[size][size] = {0}; 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]; if (n < 10) cout << n << " " ; else cout << n << " " ; } cout << endl; } } // Driver Code int main() { // Get the size of size int size = 5; // Print the Spiral Pattern printSpiral(size); return 0; } // This code is contributed by 29AjayKumar |
chevron_right
filter_none
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); } } |
chevron_right
filter_none
C#
// C# implementation of the approach using System; 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]; Console.Write((n < 10) ? (n + " " ) : (n + " " )); } Console.WriteLine(); } } // Driver Code public static void Main(String[] args) { // Get the size of size int size = 5; // Print the Spiral Pattern printSpiral(size); } } /* This code contributed by PrinciRaj1992 */ |
chevron_right
filter_none
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
Recommended Posts:
- Spiral Pattern
- Program to print Hut Star pattern
- Program to Print the Trapezium Pattern
- Program to print Inverse Diamond pattern
- Program to print numbers with diamond pattern
- Java Program to Print a Square Pattern for given integer
- Print a given matrix in spiral form
- Print K'th element in spiral form of matrix
- Print n x n spiral matrix using O(1) extra space
- Print a given matrix in reverse spiral form
- Print a matrix in a spiral form starting from a point
- Print a given matrix in counter-clock wise spiral form
- Print a given matrix in spiral form using direction tracking method
- Print matrix in diagonal pattern
- Print matrix in snake pattern
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.