Learn / Arrays

Matrix traversal

A matrix is a grid of rows and columns. Most matrix problems are about visiting its cells in a planned order: row by row, layer by layer like a spiral, or from one corner backwards. Pascal's triangle is the same idea on a triangle-shaped grid, where each cell is built from the row above.

How to recognise it

  • The input is a 2D grid and the answer depends on the order you visit cells (spiral, diagonal, layer by layer).
  • The grid must be changed in place, such as a rotation, with O(1) extra memory.
  • Each cell's value is built from cells already filled (the row above, the cell to the right or below).
  • A pair of cells must satisfy an order rule, such as one strictly below and to the right of the other.

The idea in one picture

Think of a matrix as a city map of R rows and C columns. The cell at row r, column c is m[r][c]. Rows go down, columns go right. Most matrix problems are not hard because of the data, but because of the route: which cell comes next, and when to stop. Write the route down as clear boundaries, and the code follows.

Three routes you will meet

RouteHow it movesProblem here
Layer by layer (spiral)Four sides of the outer ring, then shrink the ringSpiral Matrix
Swap across a lineSwap m[i][j] with m[j][i], then flip each rowRotate Image
Backwards from a cornerFill each cell from the cells below and to its rightFind a specific pair

Template: the spiral with four boundaries

top = 0, bottom = R - 1, left = 0, right = C - 1
while top <= bottom and left <= right:
    read row top from left to right;        top = top + 1
    read column right from top to bottom;   right = right - 1
    if top <= bottom:  read row bottom from right to left;  bottom = bottom - 1
    if left <= right:  read column left from bottom to top;  left = left + 1

The two if checks matter when only one row or one column is left. Without them the last line is read twice.

Pascal's triangle: a grid built from the row above

Row r has r + 1 numbers. The edges are 1, and every inner number is the sum of the two numbers just above it.

rowvalues
01
11 1
21 2 1
31 3 3 1
41 4 6 4 1

The value at row r, column c is the binomial number C(r, c). So a row can also be built on its own, left to right, using C(r, c + 1) = C(r, c) × (r - c) / (c + 1). Multiply first, then divide, and the division is always exact.

Common mistakes

  • Swapping m[r][c] around as m[c][r] in a matrix that is not square. Rows and columns have different lengths.
  • Transposing with both loops over the full range, which swaps every pair twice and changes nothing.
  • Reading a corner cell twice in a spiral because the boundary moved after the loop instead of before the next side.
  • Building Pascal values with int products. The product can overflow before the division, so use a 64-bit value.

Problems

0 of 6 solved

Check yourself

In the spiral walk, after the top row and the right column are read, why check top <= bottom before reading the bottom row?

Which two in-place steps rotate a square matrix 90 degrees clockwise?

Row 5 of Pascal's triangle (counting from row 0) is 1 5 10 10 5 1. How is the 10 at column 2 found from the 5 at column 1 without the row above?

For the "specific pair" problem you need, for every cell, the largest value strictly below and to the right. Which order fills that table in one pass?