How to Make a Spiral in Java


The challenge

Your task, is to create a NxN spiral with a given size.

For example, spiral with size 5 should look like this:

00000
....0
000.0
0...0
00000

and with the size 10:

0000000000
.........0
00000000.0
0......0.0
0.0000.0.0
0.0..0.0.0
0.0....0.0
0.000000.0
0........0
0000000000

Return value should contain array of arrays, of `` and 1, for example for given size 5 result should be:

[[1,1,1,1,1],[0,0,0,0,1],[1,1,1,0,1],[1,0,0,0,1],[1,1,1,1,1]]

Because of the edge-cases for tiny spirals, the size will be at least 5.

General rule-of-a-thumb is, that the snake made with ‘1’ cannot touch to itself.

The solution in Java code

Option 1:

public class Spiralizor {

    public static int[][] spiralize(int size) {
        if(size <= 0) return null;
        int[][] spiral = new int[size][size]; 
        int minCol = 0;
        int maxCol = size-1;
        int minRow = 0;
        int maxRow = size-1;
        
        for (int i = 0; i < spiral.length; i++) {
            for (int j = 0; j < spiral.length; j++) spiral[i][j] = 0;
        }
        
        while (minRow <= maxRow){
            for (int i = minCol; i <= maxCol; i++) spiral[minRow][i] = 1;
             
            for (int i = minRow; i <= maxRow; i++) spiral[i][maxCol] = 1;
            
            if(minCol != 0) minCol+=1;
            if(maxRow-1 == minRow) break;
            
            for (int i = maxCol-1; i >= minCol; i--) spiral[maxRow][i] = 1;  
            
            for (int i = maxRow-1; i >= minRow+2; i--) spiral[i][minCol] = 1;

            minCol+=1;
            minRow+=2;
            maxCol-=2; 
            maxRow-=2;
        }
        return spiral;
    }
}

Option 2:

public class Spiralizor {
    public static int[][] spiralize(int n) {
      int [][] a = new int[n][n];
      int n4 = n%4==0? 1: 0;
      for (int y = 0, c = 1; y <= n/2; y++) {
        for (int x = y; x < n -y; x++)
          a[y][x] = a[x][n-1 -y] = a[n-1 -y][n-1 -x] = a[n-1 -x][y] = c;
          c = 1-c;
        if (y+n4 < n/2) a[y+1][y] = c;
      }
      return a;
    }
}

Test cases to validate our solution

import org.junit.Test;

import static org.junit.Assert.assertArrayEquals;

public class SpiralizorTests {

    @Test
    public void test5() {
        assertArrayEquals(
                new int[][] {
                        { 1, 1, 1, 1, 1 },
                        { 0, 0, 0, 0, 1 },
                        { 1, 1, 1, 0, 1 },
                        { 1, 0, 0, 0, 1 },
                        { 1, 1, 1, 1, 1 }
                },
                Spiralizor.spiralize(5));
    }

    @Test
    public void test8() {
        assertArrayEquals(
                new int[][] {
                        { 1, 1, 1, 1, 1, 1, 1, 1 },
                        { 0, 0, 0, 0, 0, 0, 0, 1 },
                        { 1, 1, 1, 1, 1, 1, 0, 1 },
                        { 1, 0, 0, 0, 0, 1, 0, 1 },
                        { 1, 0, 1, 0, 0, 1, 0, 1 },
                        { 1, 0, 1, 1, 1, 1, 0, 1 },
                        { 1, 0, 0, 0, 0, 0, 0, 1 },
                        { 1, 1, 1, 1, 1, 1, 1, 1 },
                },
                Spiralizor.spiralize(8));
    }

}