GATE Computer ScienceHardGATE CS (PYQ pattern)

int a[] = {1,1,2,2,2,3,4,4,5,5,5,5,6}; int w = 2; for (int i = 2; i < 13; i++) if (a[i] != a[w - 2]) a[w++] = a[i]; What is w after the loop, and what is the sum of a[0..w-1]?

  1. A. 6 and 21
  2. B. 11 and 38
  3. C. 10 and 30
  4. D. 10 and 33

Correct answer: D. 10 and 33

This keeps at most two copies of each value: the kept prefix is [1, 1, 2, 2, 3, 4, 4, 5, 5, 6] (length 10).

More Programming and Data Structures (GATE) questions

Exams that ask this topic