ProgrammingHard

What is the most efficient way to check whether an n x n matrix is symmetric in O(n^2)?

  1. A. Compare every element with every other element, O(n^4)
  2. B. Compare a[i][j] with a[j][i] for all j > i
  3. C. Sort the matrix row by row and compare the rows
  4. D. Check only the diagonal elements

Correct answer: B. Compare a[i][j] with a[j][i] for all j > i

Comparing each pair once with j > i performs about n^2/2 comparisons. The diagonal alone does not decide symmetry.

More C Programming questions

Exams that ask this topic