1

Given a matrix of m * n elements (m rows, n columns), return all elements of the matrix in spiral order.

code is not working for large inputs, in fact i suspect something is wrong with last for loop in code where it is running 1 extra time.

    public int[] spiralOrder(final int[][] A) {
                int m=A.length;
                int n=A[0].length;
                int t=0, b=m-1, l=0, r=n-1, dir=0;
                int[] arr = new int[m*n];
                int p=0;
                while(t<=b && l<=r){

                    if(dir==0){
                        for(int i=l; i<=r ; i++)
                        {
                            arr[p] = A[t][i];
                            p++;
                        }
                        t++;
                        dir = 1;
                    }
                    else if(dir==1){
                        for(int j=t; j<=b; j++)
                        {
                            arr[p] = A[j][r];
                            p++;
                        }
                        dir = 2;
                        r--;
                    }
                    else if(dir==2){
                        if (r<0)break;
                        for(int i=r; i>=l; i--){
                            arr[p]=A[b][i];
                            p++;
                        }
                        dir=3;
                        b--;
                    }
                    else if(dir==3){
                        if (b<0) break;
                        for(int j = b; b>=t; j--){
                            if(j<0)break;
                            else if(j==0 & l==0)break;
                            else{
                                arr[p]=A[j][l];
                                p++;
                            }
                        }
                        dir=0;
                        l++;
                    }
                }
                return arr;
            }

Input Array:

[[150, 6, 240, 129, 168, 346, 218, 168, 309, 242, 26, 327][98, 275, 315, 389, 270, 2, 172, 100, 151, 41, 217, 176][267, 5, 324, 344, 134, 122, 229, 196, 225, 280, 200, 274][155, 320, 8, 215, 273, 291, 174, 165, 279, 26, 327, 214][207, 91, 121, 46, 125, 247, 303, 387, 214, 249, 97, 316]]

Expected Result:

150 6 240 129 168 346 218 168 309 242 26 327 176 274 214 316 97 249 214 387 303 247 125 46 121 91 207 155 267 98 275 315 389 270 2 172 100 151 41 217 200 327 26 279 165 174 291 273 215 8 320 5 324 344 134 122 229 196 225 280

Actual Result:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 60 at Solution.spiralOrder(Solution.java:16) at in.main(Main.java:194)

Pradeep
  • 9,667
  • 13
  • 27
  • 34

1 Answers1

0

The reason is because your p continues to grow (p++) even past the capacity of A.

In your case you are trying to populate A[60] with a number but A only goes until A[59] because it has 60 places, including A[0].

A only contains mn places, so you must condition the first loop to run only if pn.

Of course to avoid making the calculation every time you can save m*n inside a variable and compare p with it.

if(dir==0){
   for(int i=l; i<=r&&p<m*n ; i++)
   {
      arr[p] = A[t][i];
      p++;
   }
   t++;
   dir = 1;
}