GATE Computer ScienceVery hardGATE CS (PYQ pattern)

int cand = -1, cnt = 0, ch = 0; int a[] = {3,3,4,2,4,4,2,4,3,3,3,1,3,2}; for (int i = 0; i < 14; i++) { if (cnt == 0) { cand = a[i]; cnt = 1; ch++; } else if (a[i] == cand) cnt++; else cnt--; } printf("%d %d %d", cand, cnt, ch); What is printed?

  1. A. 3 1 4
  2. B. 4 0 4
  3. C. 3 0 4
  4. D. 3 0 5

Correct answer: C. 3 0 4

Trace the pair-cancellation: the candidate changes 4 times, ending with candidate 3 and counter 0. (The candidate is not guaranteed to be a majority.)

More Programming and Data Structures (GATE) questions

Exams that ask this topic