ProgrammingHard
What is the most efficient way to check whether an n x n matrix is symmetric in O(n^2)?
- A. Compare every element with every other element, O(n^4)
- B. Compare a[i][j] with a[j][i] for all j > i
- C. Sort the matrix row by row and compare the rows
- 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
- A 3 x 4 matrix holds a[i][j] = i 4 + j + 1. What is the sum of its boundary cells (first and last row, first and last column)?
- The number of ways to tile a 2 x 5 board exactly with 2 x 1 dominoes, counting orientations, is what?
- The length of the longest common subsequence of "ABCBDAB" and "BDCAB" is what?
- What is the Levenshtein edit distance between "kitten" and "sitting" (insert, delete, replace, each cost 1)?
- A 5 x 7 int matrix is stored in row-major order. What is the flat offset of the cell at 1-based row 3, column 4?
- On a 4 x 4 grid, a robot moves only right or down from (0,0) to (3,3) and may not enter cell (1,1). How many routes are there?