Print All Elements of Two Dimensional Array in Spiral

Objective: This question was asked in Amazon interview for the Software development Engineer position, Write an algorithm to print all the elements of two dimensional array in spiral.
Example :

Print 2D array in Spiral
Print 2D array in Spiral

Input: Two dimensional array

Output: All array elements printed in spiral.

Approach:

  • Start printing from first row.
  • Print row and columns, forward and backward alternatively
  • With every iteration of (either row or column), reduce the size of an row or column by 1
  • Call recursively

Complete Code:

public class Print2DArrayInSpiral {
public int arrA[][] = { { 1, 2, 3, 4, 5 }, { 18, 19, 20, 21, 6 },
{ 17, 28, 29, 22, 7 }, { 16, 27, 30, 23, 8 },
{ 15, 26, 25, 24, 9 }, { 14, 13, 12, 11, 10 } };
public int printSpiral(int row_S, int row_E, int col_S, int col_E,
boolean reverse, boolean rowPrint) {
if (row_S > row_E && col_S > col_E) {
return 1;
}
if (rowPrint == false) {
if (reverse == false) {
for (int i = col_S; i <= col_E; i++) {
System.out.print(" " + arrA[row_S][i]);
}
}
row_S++;
rowPrint = true;
reverse = false;
}
if (rowPrint == true) {
if (reverse == false) {
for (int i = row_S; i <= row_E; i++) {
System.out.print(" " + arrA[i][col_E]);
}
}
col_E;
rowPrint = false;
reverse = true;
}
if (rowPrint == false) {
if (reverse == true) {
for (int i = col_E; i >= col_S; i) {
System.out.print(" " + arrA[row_E][i]);
}
}
row_E;
rowPrint = true;
reverse = true;
}
if (rowPrint == true) {
if (reverse == true) {
for (int i = row_E; i >= row_S; i) {
System.out.print(" " + arrA[i][col_S]);
}
}
col_S++;
rowPrint = false;
reverse = false;
}
printSpiral(row_S, row_E, col_S, col_E, reverse, rowPrint);
return 0;
}
public static void main(String args[]) {
Print2DArrayInSpiral p = new Print2DArrayInSpiral();
p.printSpiral(0, 5, 0, 4, false, false);
}
}

Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30