Spiral traversal of a matrix

Given a 2d matrix of dimension n*m, the task is to print the matrix in spiral order. (To understand what is spiral order please refer to the diagram) For example:

Input:
3 3
1 2 3
4 5 6
7 8 9

Output: 
1 2 3 6 9 8 7 4 5

Explanation: The below diagram clearly shows the order of printing to be followed.

Spiral traversal of a matrix thought Process

1. The number of elements to be printed is n*m. We will maintain a count variable to keep track of the number of elements printed till now.

2. There are four directions in which we need to print the elements as shown below. The numbering indicates the order of printing.

3. For each direction, we need start and end to print the elements. Let’s maintain four variables, row, col, rowe, cole.

row = row starting index
col = column starting index
rowe = row ending index
cole = col ending index

Let, row = 0, col = 0, rowe = 2, cole = 2, After handling any row or column, we need to update the variables.

4. To handle rows like this,
Spiral traversal of a matrix

We will print elements from col to cole i.e, [0,2] with the row being fixed. After printing each row, we will update the row to row + 1.
Now, row = 1
5. To handle column like this,

Spiral traversal of a matrix

We will print elements from row to rowe i.e, [1,2] with cole being fixed. After printing each column, we will update the cole to cole – 1. Now, cole = 1

6. To handle rows like this,

We will print elements from cole to col i.e, [1,0] with the row being fixed. After printing each row, we will update the rowe to rowe – 1. Now, rowe = 1

7. To handle columns like this,

We will print elements from row to rowe i.e, [1,1] with col being fixed. After printing each column, we will update the col to col + 1.
Now, col = 1

Points 4,5,6,7 explains four operations. These four operations need to be performed in a loop until the number of elements printed reaches n*m. As soon as printed elements reach n*m, break out of the loop.

Any corner cases or where you can make mistakes? Yes. Take care of the cases where the number of rows is 0 and check whether the number of printed elements reaches n*m or not in every loop.

The code is given below:

public class Solution {

    public static void spiralPrint(int a[][]){
        
        int n = a.length;
        
        if(n == 0)
            return;
        
        int m = a[0].length;
        
        int count = n*m;
        int num = 0;
        
        int row = 0, col = 0;
        int rowe = n-1, cole = m-1;
        
        while(num < count)
        {
         // step 4
            for(int i = col; i <= cole && num < count; i++)
            {
                System.out.print(a[row][i] + " ");
                num++;
            }
            row++;
           // step 5 
            for(int i = row; i <= rose && num = col 
                        && num = row && num < count; i--)
            {
                System.out.print(a[i][col] + " ");
                num++;
            }
            col++;
        }

    }
}


The time complexity of the implementation is O(n*m) as we are doing constant work on every index whereas space complexity is O(1) as no extra space is used.

This article is contributed by Mukul, if you also contribute to Algorithms and Me, please check out our publisher’s program.