Zig-zag scan an N x N array
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
A zig-zag scan visits an N x N matrix along diagonals, alternating direction each time. This traversal appears in signal-processing and compression workflows because it groups nearby low-frequency positions early, but it is also a useful general matrix traversal problem.
The Core Observation
Every diagonal in a matrix has the same value of row + col. That means you can iterate over diagonal sums from 0 to 2N - 2 and collect the cells belonging to each diagonal.
The zig-zag behavior comes from alternating how you output those diagonals:
- one diagonal is emitted upward
- the next is emitted downward
This is much easier to reason about than hand-writing a long sequence of movement rules.
Python Implementation
Output:
This approach is clean because each element is visited exactly once.
Why the Direction Alternates
If you list matrix positions by diagonal sum, the diagonals are:
- sum
0:(0, 0) - sum
1:(0, 1),(1, 0) - sum
2:(0, 2),(1, 1),(2, 0)
To get the standard zig-zag pattern, reverse every other diagonal before appending it.
That is what the if s % 2 == 0: diagonal.reverse() line is doing.
Inverse Operation: Rebuild the Matrix
If you have the zig-zag sequence and want to reconstruct the matrix, use the same diagonal traversal order in reverse.
A good test is that zigzag_unscan(zigzag_scan(matrix), n) should reproduce the original matrix.
Complexity
The scan runs in O(N^2) time because the matrix has N^2 elements and each one is touched once.
The extra memory is O(N) for the temporary diagonal list in the straightforward implementation.
That is efficient enough for most practical matrix sizes.
Where Zig-Zag Order Is Used
The classic example is JPEG-style coefficient ordering after block transforms. In that context, zig-zag order tends to place many low-frequency values first and long runs of zeros later, which helps downstream run-length encoding.
Even outside compression, it is a neat exercise in structured traversal because it rewards finding the underlying diagonal pattern instead of manually steering across the grid.
Common Pitfalls
A common mistake is trying to move cell by cell with many edge-condition branches. That usually produces off-by-one bugs.
Another pitfall is forgetting to validate that the matrix is square. A zig-zag scan of a general rectangular matrix is possible, but it is a different problem from the one stated here.
Developers also sometimes reverse the wrong diagonal parity and get a mirrored traversal order.
Finally, always test tiny sizes such as 1 x 1, 2 x 2, and 3 x 3. Small cases expose traversal mistakes quickly.
Summary
- A zig-zag scan traverses matrix diagonals in alternating directions.
- Diagonal grouping works because cells on the same diagonal share
row + col. - A diagonal-sum loop gives a clean
O(N^2)solution. - Reversing every other diagonal produces the standard zig-zag order.
- Small boundary tests are the fastest way to validate correctness.
Related reading

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.