Spiral Matrix Problem (2d Arrays)

Spiral Matrix Problem (2d Arrays)

·

2 min read

Guys this is one of the most asked problems in company interviews, previously it was asked by Flipkart, PayPal and many more.

So let's start with the problem.

In this problem, you will be given one matrix,let's say the matrix is

{{ 1, 2, 3, 4}

{ 5, 6, 7, 8}

{ 9,10,11,12}

{13,14,15,16}}

and the output expected here is in the spiral form that is 1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.

The approach to this problem can be :

  1. Think about how we can print the output.

    The output can be printed in 4 parts, first is by printing top elements.

    second by printing the right elements.

    third by printing the bottommost elements.

    fourth by printing the left elements.

    The printing can be done by traversing the matrix from the first row(start row) then the end column then the end row and at last by traversing the first column(start column).

    For traversing, we will use "for loop" 4 times.

  2. The traversal may look like this,

    ```java //top

             for(int j=startcol;j<=endcol;j++)//travelling from starting               column to end column
            {
                 System.out.println(matrix[startrow][j]+" ");//printing elements in starting row
             }
    
            //right
             for(int i=startrow+1;i<=endrow;i++){
                 System.out.println(matrix[i][endcol]+" ");
             }


             //bottom
             for(int j=endcol-1;j>=startcol;j--){
                 System.out.println(matrix[startrow][j]+" ");
             }

             //left
             for(int i=endrow-1;i>=startrow+1;i--){
                 System.out.println(matrix[i][startcol]+" ");

             }
```
  1. Each time after the traversal, we need to Update the variables used for traversal that is startrow++,endrow--,startcol++,endcol--.

  2. Write down the rough structure of the solution,which may look like this

    while(condition) {
    1.top

    2.right

    3.bottom

    4.left

    5.update

    }

  3. Now let's talk about the important part which is condition,

    Most of the students write the condition as:

    startrow<endrow && startcol<endcol

    The above condition is only valid for the m*m matrix and sometimes won't give the correct output.

    but startrow<=endrow && startcol<=endcol

    this condition is valid for the n*m matrix and always gives the correct output.

  4. there are chances that while traversal one element can be printed twice, so to eradicate that we use the below code:

     //bottom
     for(int j=endcol-1;j>=startcol;j--){
         if(startrow==endrow){
             break;
         }
         System.out.print(matrix[endrow][j]+" ");
    
     }
     //left
     for(int i=endrow-1;i>=startrow+1;i--){
         if( startcol==endcol){
             break;
         }
         System.out.print(matrix[i][startcol]+" ");
    
     }
    
  5. At last, write the program.

PROGRAM:

public class TwoDarrays {
    public static void main(String [] args){
        int matrix[][]={{1,2,3,4},
        {5,6,7,8},
        {9,10,11,12},
        {13,14,15,16}};

spiralmatrix(matrix);
    }
    public static void spiralmatrix(int matrix[][]){
        int startrow=0;
        int startcol=0;
        int endrow=matrix.length-1;
        int endcol=matrix[0].length-1;
         while (startrow <= endrow && startcol<=endcol){
             //top
             for(int j=startcol;j<=endcol;j++){
                 System.out.print(matrix[startrow][j]+" ");
             }
             //right
             for(int i=startrow+1;i<=endrow;i++){
                 System.out.print(matrix[i][endcol]+" ");
             }
             //bottom
             for(int j=endcol-1;j>=startcol;j--){
                 if(startrow==endrow){
                     break;
                 }
                 System.out.print(matrix[endrow][j]+" ");

             }
             //left
             for(int i=endrow-1;i>=startrow+1;i--){
                 if( startcol==endcol){
                     break;
                 }
                 System.out.print(matrix[i][startcol]+" ");

             }
             startcol++;
             startrow++;
             endrow--;
             endcol--;
         }
        System.out.println();
    }

I Hope that you guys have enjoyed reading and learning the concept.

Stay connected for more blogs like this.