GATE Computer ScienceHardGATE CS (PYQ pattern)

int m[4][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}}; for (int i = 0; i < 4; i++) for (int j = 0; j < i; j++) { int t = m[i][j]; m[i][j] = m[j][i]; m[j][i] = t; } for (int i = 0; i < 4; i++) for (int j = 0, k = 3; j < k; j++, k--) { int t = m[i][j]; m[i][j] = m[i][k]; m[i][k] = t; } int s = 0; for (int i = 0; i < 4; i++) s += m[i][i] * (i + 1); What is s?

  1. A. 100
  2. B. 110
  3. C. 36
  4. D. 70

Correct answer: D. 70

Transposing and then reversing every row rotates the matrix 90 degrees clockwise: row i of the result is column i of the original read bottom to top, so m = [13 9 5 1], [14 10 6 2], [15 11 7 3], [16 12 8 4]. The main diagonal is 13, 10, 7, 4, and s = 13 + 20 + 21 + 16 = 70. Skipping the transpose (only reversing rows) gives 100, as does an anticlockwise rotation; stopping after the transpose leaves the diagonal 1, 6, 11, 16 and gives 110.

More Programming and Data Structures (GATE) questions

Exams that ask this topic