DSA SheetLesson · no judge

DSA FUNDAMENTALSMATRIX BASICS

Check if a Matrix is Symmetric

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

Symmetry compares swapped indices, not reversed rows

A matrix is symmetric when reflecting it across its main diagonal leaves every value unchanged. For each row index i and column index j, the required condition is A[i][j] == A[j][i]. Matrix A is square because it has n = 3 rows and 3 columns, so every swapped position exists inside the matrix.

The rows of A are [1, 2, 1], [2, 4, 2], and [5, 6, 5]. Each row reads the same from left to right and right to left, but that only compares cells within one row. Matrix symmetry compares a cell in one row with a cell in a different row after swapping its two indices. Palindromic rows do not prove symmetry.

the indexed cells of A reflected across its main diagonalA 3 x 3 indexed grid contains rows [1, 2, 1], [2, 4, 2], and [5, 6, 5]. The main diagonal cells A[0][0], A[1][1], and A[2][2] map to themselves. A[0][1] = 2 is connected to A[1][0] = 2 as an equal mirrored pair. A[0][2] = 1 is connected to A[2][0] = 5 as an unequal mirrored pair. A[1][2] = 2 is connected to A[2][1] = 6 as the remaining mirrored pair.1self2124self2565selfequal: 2 = 2unequal: 1 ≠ 5unequal: 2 ≠ 6A: indexed cells and transposed pairscol 0col 1col 2row 0row 1row 2Swap the indices: A[i][j] pairs with A[j][i]. Row-wise reversal is a different operation.
Reflection across the main diagonal swaps the two indices.
CHECKPOINT 1Not answered

Which cell must be compared with A[0][2] in matrix A?

Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.

The first unequal mirrored pair fixes the answer

Start with the cells above the main diagonal and compare each one with its swapped-index partner. The first comparison is A[0][1] = 2 against A[1][0] = 2, so this mirrored pair matches. The next comparison is A[0][2] = 1 against A[2][0] = 5, so the matrix is not symmetric.

Once one mirrored pair has different values, the entire matrix fails the test. The remaining pair A[1][2] = 2 and A[2][1] = 6 also differs, but checking it cannot change the answer from false to true. A symmetric matrix must pass every mirrored-pair comparison.

The upper triangle checks every mirrored pair exactly once

For each row i, start the column index j at i + 1 and continue while j < n. This visits only cells above the main diagonal. Compare A[i][j] with A[j][i], and return false when they differ.

CPPThe inner loop compares each upper-triangle cell with its swapped-index partner.
for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
        if (A[i][j] != A[j][i]) {
            return false;
        }
    }
}
return true;

A diagonal cell such as A[1][1] maps to itself, so comparing it with itself cannot reveal a mismatch. Starting j at i + 1 skips these self-comparisons. The lower triangle would only repeat the same pairs in reverse order: after checking A[0][2] against A[2][0], checking A[2][0] against A[0][2] adds no information.

CHECKPOINT 2Not answered

Complete the inner-loop start so each mirrored pair in A is checked once: for (j = ___; j < n; j++)

for (j = ___; j < n; j++)

Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.

Early return saves comparisons without changing the test

For A, the algorithm returns false after comparing only two off-diagonal pairs. It first sees that 2 equals 2, then sees that 1 does not equal 5, so it stops immediately. A successful check cannot stop early, because it must confirm that every mirrored pair matches.

A square matrix with n rows has n(n - 1) / 2 off-diagonal mirrored pairs in its upper triangle. A full check therefore takes O(n^2) time. The algorithm stores only the loop indices and a few temporary values, so its extra space is O(1).

Previous · Print the Matrix in a Zig-Zag PatternNext part · Quiz