@@ -0,0 +1,2652 @@
+import java.util.Arrays;%0A%0Apublic class Spiral %7B%0A%09%0A%09  private enum Direction %7B %0A%09      UP, DOWN, LEFT, RIGHT %0A%09  %7D%0A%0A%09  private boolean isCoordinateValid(int height, int width, int row, int column) %7B%0A%09      return (row %3E 0 && row %3C= height) && (column %3E 0 && column %3C= width);%0A%09  %7D%0A%0A%09  private int getNumberAtCoordinate(int width, int row, int column) %7B%0A%09      return column + (row - 1) * width; %0A%09  %7D%0A%0A%09  private int%5B%5D getNextCoordinate(int row, int column, Direction currentDirection) %7B%0A%09      int%5B%5D nextCoordinate = new int%5B2%5D;%0A%09      nextCoordinate%5B0%5D = row;%0A%09      nextCoordinate%5B1%5D = column;%0A%09      switch (currentDirection) %7B%0A%09          case UP: %0A%09              nextCoordinate%5B0%5D -= 1; %0A%09              break;%0A%09          case LEFT: %0A%09              nextCoordinate%5B1%5D -= 1;%0A%09              break;%0A%09          case DOWN: %0A%09              nextCoordinate%5B0%5D += 1;%0A%09              break;%0A%09          case RIGHT:%0A%09              nextCoordinate%5B1%5D += 1;%0A%09              break;%0A%09      %7D%0A%09      return nextCoordinate;%0A%09  %7D %0A%0A%09  private Direction getNextDirection(Direction currentDirection) %7B%0A%09      switch (currentDirection) %7B%0A%09        case UP:%0A%09            return Direction.LEFT;%0A%09        case LEFT:%0A%09            return Direction.DOWN;%0A%09        case DOWN:%0A%09            return Direction.RIGHT;%0A%09        case RIGHT:%0A%09            return Direction.UP;%0A%09        default:%0A%09            return currentDirection;%0A%09      %7D%0A%09  %7D%0A%0A%09  public int%5B%5D spiral(int height, int width, int row, int column) %7B%0A%09      int numberOfElements = height * width;%0A%09      int%5B%5D output = new int%5BnumberOfElements%5D; %0A%09      int outputIndex = 0;%0A%09      Direction currentDirection = Direction.UP;%0A%09      int stepsTaken = 0;%0A%09      int stepsNeeded = 1;%0A%09      while (outputIndex %3C numberOfElements) %7B%0A%09          if (isCoordinateValid(height, width, row, column)) %7B%0A%09              output%5BoutputIndex++%5D = getNumberAtCoordinate(width, row, column);%0A%09          %7D%0A%09          int%5B%5D nextCoordinate = getNextCoordinate(row, column, currentDirection);%0A%09          row = nextCoordinate%5B0%5D;%0A%09          column = nextCoordinate%5B1%5D;%0A%09          ++stepsTaken;%0A%09          if (stepsTaken == stepsNeeded) %7B%0A%09              if (currentDirection == Direction.LEFT %7C%7C currentDirection == Direction.RIGHT) %7B%0A%09                  ++stepsNeeded;%0A%09              %7D%0A%09              stepsTaken = 0;%0A%09        %09  currentDirection = getNextDirection(currentDirection);%0A%09          %7D%0A%09      %7D %0A%09      return output;%0A%09  %7D%09%0A%09  %0A%09  public static void main(String%5B%5D args) %7B%0A%09%09  Spiral s = new Spiral();%0A%09%09  System.out.println(Arrays.toString(s.spiral(5, 5, 3, 3)));%0A%09%09  System.out.println(Arrays.toString(s.spiral(2, 4, 1, 2)));%0A%09%09  %0A%09  %7D%0A%7D%0A

Create spiral.java