DSA FUNDAMENTALS › MATRIX BASICS
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.
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.
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.
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.
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.
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.
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).