<>DEVKIT
Algorithm StudioInterview Prep
DevkitAlgorithm Visualization & Interview Prep
Algorithm StudioInterview Prep
← All patterns
math

Math & Geometry

Mathematical operations including modular arithmetic, matrix operations, and computational geometry.

DIFFICULTYmedium
TIMEO(m × n)
SPACEO(1)
STATUSnot started

What are Math and Geometry Patterns?

Math and geometry problems become manageable when positions and transformations are written as precise rules. For matrices, the useful state is often a set of boundaries or coordinates rather than the values alone.

You need row and column indexes, loops, and basic arithmetic. The short version is: name the coordinates, write how they change, and protect any value that will be overwritten.

The Core Idea: Track the Remaining Shape

In LC 54: Spiral Matrix, the 3 by 3 matrix is read across the top, down the right side, across the bottom, and up the left side. After that outer layer is finished, only the center remains.

How do we know which cells are still unprocessed? Four boundaries describe them: top, bottom, left, and right. After finishing one side, we move its boundary inward.

It is like peeling an onion one layer at a time. Once the outer layer is removed, the same four-sided shape remains inside.

How Spiral Boundaries Move

The Rule You Keep True

The four boundaries enclose exactly the cells that have not been visited yet. Every traversal must shrink one boundary before another side is read.

The state looks different for other problems:

  • Spiral traversal shrinks four boundaries.
  • Rotate Image moves four coordinates in one cycle before continuing.
  • Set Matrix Zeroes stores row and column markers before clearing cells.
  • Happy Number follows a numeric transformation and checks whether it reaches 1 or repeats.

LC 73: Set Matrix Zeroes is the one that usually trips us up. Clearing cells immediately destroys information about which zeroes were in the original matrix. Mark first, then clear in a separate pass.

Picking the Right Shape

From Rule to Code

  1. Define what each coordinate, boundary, or numeric state means.
  2. Record any information that an in-place update would destroy.
  3. Apply one complete traversal side, swap cycle, or transformation step.
  4. Update the boundaries or state only after that work is complete.
  5. Stop when the remaining region is empty or a terminal state or cycle is found.

Matrix operations usually visit each cell a constant number of times, giving O(rows × columns) time.

When to use it

  • •The problem follows rows, columns, layers, coordinates, or geometric transformations.
  • •A matrix must be traversed or modified in place.
  • •The next state follows a mathematical rule rather than an arbitrary graph edge.
  • •Repeated arithmetic states may form a cycle.

How to identify it

  • •The input is a matrix, point list, number transformation, or coordinate system.
  • •The prompt asks to rotate, spiral, reflect, mark rows, or detect a numeric cycle.
  • •Boundaries or coordinates describe the unprocessed region.
  • •A small formula maps the current position to its next position.

Key insights

  • →Write the coordinate transformation before moving any matrix values.
  • →Boundaries such as top, bottom, left, and right describe the remaining matrix layer.
  • →In-place matrix changes often require a temporary value or a sequence of swaps.
  • →Marker rows and columns can record which lines must change without extra matrix-sized storage.
  • →A repeated numeric state can be detected with a set or slow and fast pointers.

Common mistakes

  1. 01Mixing row and column indexes in a coordinate formula.
  2. 02Processing the same row or column twice when a spiral closes to one line.
  3. 03Overwriting a matrix value before its original value has been moved elsewhere.
  4. 04Using a transformed first row as a marker without remembering whether it originally contained zero.
  5. 05Assuming every repeated number process reaches the desired value instead of a cycle.
Theory map
What are Math and Geometry Patterns?The Core Idea: Track the Remaining ShapeHow Spiral Boundaries MoveThe Rule You Keep TruePicking the Right ShapeFrom Rule to CodeKey insightsCommon mistakes