GATE Computer ScienceHardGATE CS (PYQ pattern)
int a[] = {1, 3, 4, 6, 8, 9, 11, 14}; int i = 0, j = 7, c = 0, it = 0; while (i < j) { it++; if (a[i] + a[j] > 15) { c += j - i; j--; } else i++; } printf("%d %d", c, it); What is printed?
- A. 13 7
- B. 7 7
- C. 10 7
- D. 10 8
Correct answer: C. 10 7
When a[i] + a[j] > 15, every index from i to j - 1 pairs with j to exceed 15 (the array is sorted), so j - i pairs are counted at once and j retires; otherwise a[i] is too small for every remaining partner and i retires. Exactly one index retires per iteration, so it = 7 and c is the number of pairs with sum above 15, which is 10 (a brute-force count agrees). Using >= 15 would give 13.
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- #define SQ(x) x x #define MAX(a, b) ((a) > (b) ? (a) : (b)) int main() { int i = 6, j = 5, k = 6; int r = MAX(i++, j); int s = MAX(j++…
- int a = -17, b = 5; unsigned u = 5; / 32-bit int and unsigned, C99 or later / printf("%d %d %u", a / b, a % -b, a / u); What is printed?
- int a = 0, b = 0, c = 0; int r = (a++ && b++) || (++c && a++) || (b++ || ++c); printf("%d %d %d %d", r, a, b, c); What is printed?
- unsigned f(unsigned x) { unsigned c = 0; while (x) { x &= x - 1; c++; } return c; } unsigned g(unsigned x) { return x & ~(x - 1); }…
- int i = 2, j = 3, k, m; k = i += j, j -= i, i j; m = (i, j); printf("%d %d %d %d", i, j, k, m); What is printed?
- int a[] = {10, 20, 30, 40}; int p[] = {a, a + 1, a + 2, a + 3}; int pp = p; int t1 = pp++; int t2 = ++pp; int t3 = ++++pp; printf("%d %d…