Home/Learn/Matrix Operations & Transformations

Pattern Guide

Matrix Operations & Transformations

"Rotate 90°, transpose, spiral traversal, search in sorted matrix."

Matrix transformation problems require understanding coordinate mappings: rotating 90° clockwise maps (r,c) → (c, n-1-r); transposing maps (r,c) → (c,r). Spiral traversal uses four shrinking boundaries. Searching in a row-and-column sorted matrix exploits monotone structure by starting at top-right corner. These operations appear frequently in simulations and geometric problems.

Problems you can solve with this pattern

4 problems · click any to start solving

All graph
1Rotate ImageMediumSolve
2Spiral MatrixMediumSolve
3Search a 2D Matrix IIMediumSolve
4Game of LifeMediumSolve
Rotate matrix and spiral order templates
// Rotate 90° clockwise in-place: transpose + reverse each row
function rotate(matrix) {
    const n = matrix.length;
    // Transpose
    for (let i = 0; i < n; i++)
        for (let j = i + 1; j < n; j++)
            [matrix[i][j], matrix[j][i]] = [matrix[j][i], matrix[i][j]];
    // Reverse each row
    for (let i = 0; i < n; i++) matrix[i].reverse();
}

// Spiral order traversal
function spiralOrder(matrix) {
    const result = [];
    let [top, bottom, left, right] = [0, matrix.length-1, 0, matrix[0].length-1];
    while (top <= bottom && left <= right) {
        for (let c = left; c <= right; c++) result.push(matrix[top][c]); top++;
        for (let r = top; r <= bottom; r++) result.push(matrix[r][right]); right--;
        if (top <= bottom) {
            for (let c = right; c >= left; c--) result.push(matrix[bottom][c]); bottom--;
        }
        if (left <= right) {
            for (let r = bottom; r >= top; r--) result.push(matrix[r][left]); left++;
        }
    }
    return result;
}

Rotate matrix 90° clockwise in-place: transpose then reverse each row. Transpose: swap matrix[i][j] with matrix[j][i] for j > i. Reverse rows: reverse each row in place. Spiral traversal: maintain top, bottom, left, right boundaries. Shrink boundaries as each layer is traversed. Search sorted matrix: start at top-right; go left if current > target, down if current < target.

Rotate matrix and spiral order templates
// Rotate 90° clockwise in-place: transpose + reverse each row
function rotate(matrix) {
    const n = matrix.length;
    // Transpose
    for (let i = 0; i < n; i++)
        for (let j = i + 1; j < n; j++)
            [matrix[i][j], matrix[j][i]] = [matrix[j][i], matrix[i][j]];
    // Reverse each row
    for (let i = 0; i < n; i++) matrix[i].reverse();
}

// Spiral order traversal
function spiralOrder(matrix) {
    const result = [];
    let [top, bottom, left, right] = [0, matrix.length-1, 0, matrix[0].length-1];
    while (top <= bottom && left <= right) {
        for (let c = left; c <= right; c++) result.push(matrix[top][c]); top++;
        for (let r = top; r <= bottom; r++) result.push(matrix[r][right]); right--;
        if (top <= bottom) {
            for (let c = right; c >= left; c--) result.push(matrix[bottom][c]); bottom--;
        }
        if (left <= right) {
            for (let r = bottom; r >= top; r--) result.push(matrix[r][left]); left++;
        }
    }
    return result;
}
Matrix rotation formulas:
- 90° clockwise: (r,c) → (c, n-1-r) = transpose + reverse rows
- 90° counter-clockwise: (r,c) → (n-1-c, r) = transpose + reverse columns
- 180°: (r,c) → (n-1-r, n-1-c) = reverse rows + reverse columns

Sorted matrix search: Top-right corner is the "pivot" — it's larger than everything to its left and smaller than everything below. Each comparison eliminates one row or column.

In-place tricks: Use bit encoding to store old and new state simultaneously. LSB = current state, MSB = next state. Read LSB for neighbor counting, right-shift at end.