DSA SheetLesson · no judge

DSA FUNDAMENTALSMATRIX BASICS

Print a Matrix in a Zig-Zag Pattern

Reading · 6 minQuiz · 5 questions2 code drills · run onlyGenerated by gpt-5.6-luna · Aug 25

A zig-zag traversal changes the visit order, not the matrix

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.

TEXTThe values stay in their cells. Only the order of visiting them changes.
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 12

The 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.

the zig-zag path through the 3 x 4 matrix [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]A 3 x 4 grid contains row 0 as 1, 2, 3, 4; row 1 as 5, 6, 7, 8; and row 2 as 9, 10, 11, 12. An arrow crosses row 0 from 1 to 4, moves to 8, crosses row 1 from 8 to 5, moves to 9, and crosses row 2 from 9 to 12. The labeled visit sequence is 1, 2, 3, 4, 8, 7, 6, 5, 9, 10, 11, 12, while each number remains in its original cell.123456789101112visit order: 1, 2, 3, 4, 8, 7, 6, 5, 9, 10, 11, 12
Only the direction of visiting cells changes from one row to the next.

The row index determines which horizontal direction to use

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.

CHECKPOINT 1Not answered

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.

Two opposite inner loops can share one outer row loop

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.

CPPC++ uses row parity to select the column direction.
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] << " ";
        }
    }
}
JAVAJava uses the same two inner-loop bounds.
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.

CHECKPOINT 2Not answered

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.

Visiting every cell once gives the zig-zag traversal its full cost

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 INDEXCOLUMN ORDERVALUES PRINTED
00, 1, 2, 31 2 3 4
13, 2, 1, 08 7 6 5
20, 1, 2, 39 10 11 12
The row bounds account for all 12 visits.

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.