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)
{
int row = 0, col = 0;
int boundary = size - 1;
int sizeLeft = size - 1;
int flag = 1;
char move = 'r' ;
int matrix[size][size] = {0};
for ( int i = 1; i < size * size + 1; i++)
{
matrix[row][col] = i;
switch (move)
{
case 'r' :
col += 1;
break ;
case 'l' :
col -= 1;
break ;
case 'u' :
row -= 1;
break ;
case 'd' :
row += 1;
break ;
}
if (i == boundary)
{
boundary += sizeLeft;
if (flag != 2)
{
flag = 2;
}
else
{
flag = 1;
sizeLeft -= 1;
}
switch (move)
{
case 'r' :
move = 'd' ;
break ;
case 'd' :
move = 'l' ;
break ;
case 'l' :
move = 'u' ;
break ;
case 'u' :
move = 'r' ;
break ;
}
}
}
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;
}
}
int main()
{
int size = 5;
printSpiral(size);
return 0;
}
|
Java
public class GFG {
public static void printSpiral( int size)
{
int row = 0 , col = 0 ;
int boundary = size - 1 ;
int sizeLeft = size - 1 ;
int flag = 1 ;
char move = 'r' ;
int matrix[][] = new int [size][size];
for ( int i = 1 ; i < size * size + 1 ; i++) {
matrix[row][col] = i;
switch (move) {
case 'r' :
col += 1 ;
break ;
case 'l' :
col -= 1 ;
break ;
case 'u' :
row -= 1 ;
break ;
case 'd' :
row += 1 ;
break ;
}
if (i == boundary) {
boundary += sizeLeft;
if (flag != 2 ) {
flag = 2 ;
}
else {
flag = 1 ;
sizeLeft -= 1 ;
}
switch (move) {
case 'r' :
move = 'd' ;
break ;
case 'd' :
move = 'l' ;
break ;
case 'l' :
move = 'u' ;
break ;
case 'u' :
move = 'r' ;
break ;
}
}
}
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();
}
}
public static void main(String[] args)
{
int size = 5 ;
printSpiral(size);
}
}
|
Python3
def printSpiral(size):
row, col = 0 , 0
boundary = size - 1
sizeLeft = size - 1
flag = 1
move = 'r'
matrix = [[ 0 for j in range (size)] for i in range (size)]
for i in range ( 1 , size * size + 1 ):
matrix[row][col] = i
if move = = 'r' :
col + = 1
elif move = = 'l' :
col - = 1
elif move = = 'u' :
row - = 1
elif move = = 'd' :
row + = 1
if i = = boundary:
boundary + = sizeLeft
if flag ! = 2 :
flag = 2
else :
flag = 1
sizeLeft - = 1
if move = = 'r' :
move = 'd'
elif move = = 'd' :
move = 'l'
elif move = = 'l' :
move = 'u'
elif move = = 'u' :
move = 'r'
for row in range (size):
for col in range (size):
n = matrix[row][col]
print (n, end = ' ' if n < 10 else ' ' )
print ()
size = 5
printSpiral(size)
|
C#
using System;
class GFG {
public static void printSpiral( int size)
{
int row = 0, col = 0;
int boundary = size - 1;
int sizeLeft = size - 1;
int flag = 1;
char move = 'r' ;
int [, ] matrix = new int [size, size];
for ( int i = 1; i < size * size + 1; i++) {
matrix[row, col] = i;
switch (move) {
case 'r' :
col += 1;
break ;
case 'l' :
col -= 1;
break ;
case 'u' :
row -= 1;
break ;
case 'd' :
row += 1;
break ;
}
if (i == boundary) {
boundary += sizeLeft;
if (flag != 2) {
flag = 2;
}
else {
flag = 1;
sizeLeft -= 1;
}
switch (move) {
case 'r' :
move = 'd' ;
break ;
case 'd' :
move = 'l' ;
break ;
case 'l' :
move = 'u' ;
break ;
case 'u' :
move = 'r' ;
break ;
}
}
}
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();
}
}
public static void Main(String[] args)
{
int size = 5;
printSpiral(size);
}
}
|
Javascript
<script>
function printSpiral(size)
{
let row = 0, col = 0;
let boundary = size - 1;
let sizeLeft = size - 1;
let flag = 1;
let move = 'r' ;
let matrix = new Array(size);
for (let i = 0; i < size; i++)
{
matrix[i] = new Array(size).fill(0);
}
for (let i = 1; i < size * size + 1; i++)
{
matrix[row][col] = i;
switch (move)
{
case 'r' :
col += 1;
break ;
case 'l' :
col -= 1;
break ;
case 'u' :
row -= 1;
break ;
case 'd' :
row += 1;
break ;
}
if (i == boundary)
{
boundary += sizeLeft;
if (flag != 2)
{
flag = 2;
}
else
{
flag = 1;
sizeLeft -= 1;
}
switch (move)
{
case 'r' :
move = 'd' ;
break ;
case 'd' :
move = 'l' ;
break ;
case 'l' :
move = 'u' ;
break ;
case 'u' :
move = 'r' ;
break ;
}
}
}
for (row = 0; row < size; row++)
{
for (col = 0; col < size; col++)
{
let n = matrix[row][col];
if (n < 10)
document.write(n + " " );
else
document.write(n + " " );
}
document.write( "<br>" );
}
}
let size = 5;
printSpiral(size);
</script>
|
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
Complexity Analysis:
- Time complexity: O(m*n) where m is number of rows and n is number of columns of a given matrix
- Auxiliary Space: O(1) because constant space has been used
Last Updated :
15 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...