package com.interview.multiarray;

/**
 * Date 03/15/2015 
 * @author tusroy
 * 
 * Given a 2D matrix(square or rectangular) print it in spiral way.
 * e.g 1 2 3
 *     4 5 6
 *     7 8 9 
 * Printing should be 1 2 3 6 9 8 7 4 5    
 *    
 * Solution:
 * Keep 4 pointers which are bounds for this matrix, up, down, left, right. Print each
 * row or column and keep incrementing and decrementing the bounds. As soon as up meets down
 * or left meets right we are done.
 * 
 * Test cases
 * Null matrix
 * Empty matrix
 * square matrix
 * rectangular matrix
 *
 * Reference
 * http://stackoverflow.com/questions/726756/print-two-dimensional-array-in-spiral-order
 * http://www.geeksforgeeks.org/print-a-given-matrix-in-spiral-form/
 */
public class SpiralPrinting {

    public void print(int matrix[][]) {
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
            return;
        }

        int left = 0;
        int right = matrix[0].length - 1;
        int up = 0;
        int down = matrix.length - 1;

        while (left < right && up < down) {
            for (int i = left; i <= right; i++) {
                System.out.print(matrix[up][i] + " ");
            }
            up++;

            for (int i = up; i <= down; i++) {
                System.out.print(matrix[i][right] + " ");
            }
            right--;

            for (int i = right; i >= left; i--) {
                System.out.print(matrix[down][i] + " ");
            }
            down--;

            for (int i = down; i >= up; i--) {
                System.out.print(matrix[i][left] + " ");
            }
            left++;
        }
    }
    
    public static void main(String args[]){
        SpiralPrinting sp = new SpiralPrinting();
        int matrix[][] = {{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15},{16,17,18,19,20}};
        sp.print(matrix);
    }
}

@@ -28,16 +28,98 @@
 array;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.Collections;%0Aimport java.util.List;%0A%0A
 /**%0A * D
@@ -149,12 +149,16 @@
 hor 
-tusr
+Tushar R
 oy%0A 
@@ -559,104 +559,65 @@
 %0A * 
-Test cases%0A * Null matrix%0A * Empty matrix%0A * square matrix%0A * rectangular matrix%0A *%0A * Reference
+Reference%0A * https://leetcode.com/problems/spiral-matrix/
 %0A * 
@@ -821,33 +821,48 @@
 lic 
-void print(int
+List%3CInteger%3E spiralOrder(int%5B%5D%5B%5D
  matrix
-%5B%5D%5B%5D
 ) %7B%0A
@@ -956,28 +956,101 @@
   return
-;%0A        %7D%0A
+ Collections.EMPTY_LIST;%0A        %7D%0A%0A        List%3CInteger%3E result = new ArrayList%3C%3E();
 %0A       
@@ -1186,16 +1186,17 @@
  (left %3C
+=
  right &
@@ -1201,16 +1201,17 @@
  && up %3C
+=
  down) %7B
@@ -1277,32 +1277,26 @@
         
-System.out.print
+result.add
 (matrix%5B
@@ -1297,30 +1297,24 @@
 atrix%5Bup%5D%5Bi%5D
- + %22 %22
 );%0A         
@@ -1400,32 +1400,26 @@
         
-System.out.print
+result.add
 (matrix%5B
@@ -1427,22 +1427,16 @@
 %5D%5Bright%5D
- + %22 %22
 );%0A     
@@ -1462,24 +1462,58 @@
   right--;%0A%0A
+            if (up %3C= down) %7B%0A    
             
@@ -1570,48 +1570,58 @@
     
-System.out.print(matrix%5Bdown%5D%5Bi%5D + %22 %22);
+    result.add(matrix%5Bdown%5D%5Bi%5D);%0A                %7D
 %0A   
@@ -1648,24 +1648,61 @@
    down--;%0A%0A
+            if (left %3C= right) %7B%0A    
             
@@ -1756,48 +1756,58 @@
     
-System.out.print(matrix%5Bi%5D%5Bleft%5D + %22 %22);
+    result.add(matrix%5Bi%5D%5Bleft%5D);%0A                %7D
 %0A   
@@ -1843,24 +1843,47 @@
 ;%0A        %7D%0A
+        return result;%0A
     %7D%0A    %0A 
@@ -1990,107 +1990,132 @@
  int
+%5B%5D%5B%5D
  matrix
-%5B%5D%5B%5D
  = %7B%7B1,
+ 
 2,
-3,4,5%7D,%7B6,7,8,9,10%7D,%7B11,12,13,14,15%7D,%7B16,17,18,19,20%7D%7D;%0A        sp.print(matrix
+ 3%7D, %7B4, 5, 6%7D%7D;%0A        List%3CInteger%3E result = sp.spiralOrder(matrix);%0A        System.out.print(result
 );%0A 

fix spiral printing logic