How to find ith item in zigzag ordering?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Zigzag ordering traverses a matrix along its diagonals, alternating between upward and downward directions. It is used in JPEG compression (DCT coefficient ordering), image processing, and interview problems. Finding the i-th element in zigzag order without generating the entire sequence requires understanding the diagonal structure — which diagonal the index falls on and the position within that diagonal.
The Zigzag Pattern
For a 4x4 matrix:
Zigzag order visits elements as: 0, 1, 4, 8, 5, 2, 3, 6, 9, 12, 13, 10, 7, 11, 14, 15.
The pattern groups elements by diagonal. Diagonal 0 has 1 element, diagonal 1 has 2, diagonal 2 has 3, up to the middle, then sizes decrease. Odd-numbered diagonals go downward (increasing row), even-numbered go upward (increasing column).
Generating Full Zigzag Order
Finding the i-th Element Directly
Instead of generating the full sequence, determine which diagonal contains index i and the position within that diagonal:
This runs in O(n + m) time without allocating the full sequence.
O(1) Direct Calculation
For a square n x n matrix, you can compute the coordinates directly:
This only works cleanly for the upper-left triangle (first n diagonals). For the full matrix, the diagonal size calculation requires handling the bottom-right triangle separately.
JPEG DCT Coefficient Order
JPEG compression uses an 8x8 zigzag scan to order DCT coefficients from low frequency (top-left) to high frequency (bottom-right):
Inverse Zigzag (Position to Index)
Given a row and column, find the zigzag index:
Common Pitfalls
- Off-by-one on diagonal direction: Even diagonals go upward, odd go downward (or vice versa depending on convention). Pick one and stay consistent — check against a small example.
- Non-square matrices: The diagonal length formula changes when rows and columns differ. A 3x5 matrix has diagonals of lengths 1, 2, 3, 3, 3, 2, 1 — not the same as a square.
- 0-indexed vs 1-indexed: Zigzag problems on interview sites may use 1-indexed positions. Clarify before coding.
- Boundary conditions: The first and last diagonals have only 1 element each. Edge cases at matrix corners (0,0) and (n-1, m-1) need special attention.
- JPEG zigzag is fixed 8x8: The JPEG standard uses a precomputed lookup table, not an algorithm. For fixed sizes, a lookup table is faster than computing coordinates.
Summary
- Zigzag ordering traverses a matrix diagonally, alternating between upward and downward directions
- Full traversal is O(n*m) — iterate diagonals 0 to n+m-2, alternating direction
- Finding the i-th element directly is O(n+m) — count diagonal sizes until you reach the target diagonal
- For square matrices, O(1) lookup is possible using the quadratic formula to find the diagonal number
- JPEG compression uses zigzag ordering on 8x8 DCT coefficient blocks
- The inverse operation (position to zigzag index) uses the same diagonal math in reverse
Related reading
- How to find largest triangle in convex hull aside from brute force search
- How to find length of digits in an integer?
- How to find list intersection?
- How to find Longest Common Substring using C
- How to find max. and min. in array using minimum comparisons?
- How to find maximum spanning tree?
- how to find longest palindromic subsequence?
- How to find minimum number of jumps to reach the end of the array in On time

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.