Print a matrix in spiral order

Hi there,

Today I’m gonna share a solution for a very common problem for programming interviews, how to print a 2D matrix in spiral order.

Problem description:

Given an M x N matrix, print all the elements in spiral order, for instance:

       [[1, 2, 3, 4]
        [2, 4, 9, 5]
        [9, 8, 7, 6]]

print: 1, 2, 3, 4, 5, 6, 7, 8, 9, 2, 4, 9

This is an easy problem which interviewers use to test how clean you are able to code and how good you are at manipulating indexes, you need to be very careful when you try to come up with a solution for this problem, remember the KISS principle: KEEP IT SIMPLE & SHORT. Talking about programming interviews, usually when your idea requires more than 30 – 40 lines of code it meas that it’s not a good solution.

Solution:

The idea is to have four indexes: top, down, left and right which are gonna be used to point where we are gonna start printing rows and columns, when top > down or left > right then we are done.

void spiralOrder(int[][] matrix)
{
     if(matrix.length == 0)
         return;
     // Initialize our four indexes
     int top = 0;
     int down = matrix.length - 1;
     int left = 0;
     int right = matrix[0].length - 1;

     while(true)
     {
         // Print top row
         for(int j = left; j <= right; ++j) System.out.print(matrix[top][j] + " ");
         top++;
         if(top > down || left > right) break;
         //Print the rightmost column
         for(int i = top; i <= down; ++i) System.out.print(matrix[i][right] + " ");
         right--;
         if(top > down || left > right) break;
         //Print the bottom row
         for(int j = right; j >= left; --j) System.out.print(matrix[down][j] + " ");
         down--;
         if(top > down || left > right) break;
         //Print the leftmost column
         for(int i = down; i >= top; --i) System.out.print(matrix[i][left] + " ");
         left++;
         if(top > down || left > right) break;
     }
 }

This solution works for any M x N matrix, time complexity: O(M * N) space complexity: Constant

Advertisements

6 thoughts on “Print a matrix in spiral order

  1. Pingback: Spiral Matrix | Data Nest

  2. Hey eveyone, this is my C implementation to print the matrix of m*n in the spiral form. Enjoy!!!

    #include
    #include
    #include

    int main(){
    int i,j,m,n,top,bottom,left,right;
    printf(“Enter m and n “);
    scanf(“%d %d”,&m,&n);
    int matrix[m][n]; // m rows and n columns
    printf(“\nEnter the array elements\n”);
    for(i=0;i<m;++i){
    for(j=0;j<n;++j){
    scanf("%d",&matrix[i][j]);
    }
    }
    printf("The array is:\n");
    for(i=0;i<m;++i){
    for(j=0;j<n;++j){
    printf("%d ",matrix[i][j]);
    }
    printf("\n");
    }
    // the code snipped to print the spiral matrix
    printf("\nThe spiral matrix is:\n");
    top = 0;
    bottom = m-1;
    left = 0;
    right = n-1;
    while(1){
    // Printing the first row
    for(i=top;ibottom || left>right) break;
    // Printing the last column
    for(j=top;jbottom || left>right) break;
    // Printing the last row
    for(i=right;i>=left;–i){
    printf(“%d “,matrix[bottom][i]);
    }
    bottom–;
    if(top>bottom || left>right) break;
    // Printing the first column
    for(j=bottom;j>=top;–j){
    printf(“%d “,matrix[j][left]);
    }
    left++;
    if(top>bottom || left>right) break;
    }
    return 0;
    }

  3. Nicely done and very readable code. Thanks.

  4. One of the greatest code I saw ever.nice code man.great work.easy to understand.i loved it.Thank you soo much.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s