We need to create an n x n grid and fill it with numbers from 1 to n^2 by walking in a clockwise spiral. The spiral starts at the top-left corner, moves right across the top row, then down the right column, then left across the bottom row, then up the left column, and repeats this pattern as it tightens inward.
The output shape is simple. The work is controlling the traversal so the path turns at the right moments and never overwrites a filled cell. One way to structure that control is to treat the spiral as a series of concentric rectangular layers. Each layer has four sides: top row, right column, bottom row, and left column. We fill one layer at a time, shrinking the boundary after each pass.
1 <= n <= 20 --> The matrix has at most 400 cells, so runtime is not the constraint that decides the approach. Correctness and boundary management are. The largest value placed is n^2 = 400, well within int range, so there is no overflow to handle.Simulate the spiral walk directly. Start at position (0, 0), move right, and keep filling numbers. When the next cell is off the edge of the matrix or already filled, turn clockwise (right turns to down, down turns to left, left turns to up, up turns to right). Continue until all n^2 cells are filled.
A direction array encodes the four moves as (row, column) deltas, and we cycle through it with an index. Turning becomes advancing that index by one, modulo four. This replaces four separate code paths for the four directions with a single rule.
Loading animation...
This approach is optimal in time. One cost is that it checks a turn condition at every cell, including reading the value of the next cell to detect a collision. The next approach removes those checks by tracking four boundaries and filling each side as a straight run between them, so it never inspects neighboring cells or tests for a turn.
Instead of simulating each step and checking whether we need to turn, we can think about the spiral as a series of concentric rectangular layers. The outermost layer is the border of the matrix. The next layer is the border of the remaining inner rectangle. And so on.
For each layer, we know exactly what to do: fill the top row left-to-right, the right column top-to-bottom, the bottom row right-to-left, and the left column bottom-to-top. After completing a layer, we shrink all four boundaries inward and repeat.
After each side is filled, its boundary moves inward by one, so the four sides of the next iteration cover cells strictly inside the previous perimeter. No cell is ever inside two different perimeters, which is why nothing is written twice. The guard needs care for odd n. When a single center cell remains, top equals bottom and left equals right. The top-row loop writes that cell, then top is incremented past bottom. The right-column loop (top..=bottom) and every later loop become empty ranges, so the center is written exactly once. The while (num <= n * n) condition then stops the outer loop instead of starting a fifth empty pass.
Loading animation...