GATE Computer ScienceModerateGATE CS (PYQ pattern)

char *s = "abracadabra"; int c[26] = {0}, i; for (i = 0; s[i]; i++) c[s[i] - 'a']++; int best = 0; for (i = 1; i < 26; i++) if (c[i] * (i + 1) >= c[best] * (best + 1)) best = i; printf("%c %d", 'a' + best, c['a' - 'a'] + c['r' - 'a']); What is printed?

  1. A. a 7
  2. B. r 7
  3. C. r 5
  4. D. d 7

Correct answer: B. r 7

Counts: a 5, b 2, r 2, c 1, d 1. Weighted c*(letter index+1): a 5, b 4, c 3, d 4, r 2*18 = 36. The maximum (with ties going to the later letter) is r; a + r counts = 5 + 2 = 7.

More Programming and Data Structures (GATE) questions

Exams that ask this topic