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?
- A. 100
- B. 110
- C. 36
- 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.
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- A routine reverses the entire string in place and then reverses each maximal run of non-space characters in place. It is applied twice in…
- A singly linked list 1 -> 2 -> ... -> 20 undergoes two passes: first every 3rd node (by current position) is deleted, then in the…
- int x = 1; int inc(void) { return x += 1; } int dbl(void) { return x = 2; } int sq(void) { return x = x; } int (fp[3])(void) = {inc, dbl…
- A table of size 13 uses double hashing, probe i of key k is (k mod 13 + i (1 + k mod 11)) mod 13. Keys 18, 41, 22, 44, 59, 32, 31, 73 are…
- A BST on 31 keys is perfectly balanced (all 5 levels full). Searching for each of the 31 keys with equal probability, what is the average…
- Keys 1 to 5 are inserted into an empty BST in a uniformly random order of the 120 possible orders. In how many of the orders does key 3…