DSA FUNDAMENTALS › MATRIX BASICS
A zig-zag traversal moves through the rows from top to bottom, but it changes horizontal direction after every row. For the matrix [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]], row 0 is visited from left to right, row 1 from right to left, and row 2 from left to right.
Matrix rows: [1 2 3 4]
[5 6 7 8]
[9 10 11 12]
Zig-zag output: 1 2 3 4 8 7 6 5 9 10 11 12The first row contributes 1 2 3 4. The next row starts at its rightmost value, so it contributes 8 7 6 5. The last row changes direction again and contributes 9 10 11 12. The matrix is never reversed, copied, or modified.
With 0-based row indices, even rows use increasing column indices and odd rows use decreasing column indices. Row 0 is even, so its columns are visited as 0, 1, 2, 3. Row 1 is odd, so its columns are visited as 3, 2, 1, 0. Row 2 is even again, so its columns are visited as 0, 1, 2, 3.
This parity check is the only decision needed for the horizontal direction. When r is even, start c at 0 and increase it while c is less than cols. When r is odd, start c at cols - 1 and decrease it while c is at least 0.
Which order should the zig-zag traversal use for row index 1 of the running matrix?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The outer loop advances r from 0 through rows - 1. For each row, an if-else selects one of two inner loops. The increasing loop starts c at 0 and stops before cols. The decreasing loop starts c at cols - 1 and stops after c reaches 0.
for (int r = 0; r < rows; r++) {
if (r % 2 == 0) {
for (int c = 0; c < cols; c++) {
cout << matrix[r][c] << " ";
}
} else {
for (int c = cols - 1; c >= 0; c--) {
cout << matrix[r][c] << " ";
}
}
}for (int r = 0; r < rows; r++) {
if (r % 2 == 0) {
for (int c = 0; c < cols; c++) {
System.out.print(matrix[r][c] + " ");
}
} else {
for (int c = cols - 1; c >= 0; c--) {
System.out.print(matrix[r][c] + " ");
}
}
}For the running matrix, r = 0 selects c = 0 through 3, printing 1 2 3 4. Then r = 1 selects c = 3 through 0, printing 8 7 6 5. Finally, r = 2 selects c = 0 through 3, printing 9 10 11 12. Every access still has the form matrix[r][c], and no cell is changed.
Complete the decreasing column loop for row 1. It must start at cols - 1, continue while c >= 0, and decrement c.
for (int c = __________; __________; __________) {
cout << matrix[1][c] << " ";
}Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The matrix has rows * cols cells, so this 3 x 4 matrix has 12 cells. Row 0 visits four cells, row 1 visits four different cells in reverse order, and row 2 visits the remaining four cells. Alternating direction does not skip or repeat a cell, because each row still visits every column exactly once.
| ROW INDEX | COLUMN ORDER | VALUES PRINTED |
|---|---|---|
| 0 | 0, 1, 2, 3 | 1 2 3 4 |
| 1 | 3, 2, 1, 0 | 8 7 6 5 |
| 2 | 0, 1, 2, 3 | 9 10 11 12 |
The outer loop runs once per row, and each inner loop runs once per column in that row. The total time is O(rows * cols), because every cell is printed once. The traversal uses O(1) extra space when it prints directly from the matrix, since it stores no reversed rows, output copy, or additional grid.