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?
- A. a 7
- B. r 7
- C. r 5
- 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.
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- For n = 7 distinct keys, what is the maximum number of swaps that the bottom-up (Floyd) build-heap for a max-heap can perform, over all…
- The stock span of day i is the number of consecutive days ending at i (including i) whose price is at most the price on day i. Prices are…
- The largest rectangle in a histogram with bar heights 2, 1, 5, 6, 2, 3, 4, 1, 3 (unit widths) is found with a stack of indices; a sentinel…
- void insert(Stack s, int x) { if (empty(s) || top(s) <= x) { push(s, x); return; } int y = pop(s); insert(s, x); push(s, y); } void…
- Kahn's topological sort uses a FIFO queue seeded with all vertices of in-degree 0 in increasing order; vertices are dequeued, and…
- An undirected graph has edges 1-2, 2-3, 3-4, 4-5, 5-6, 6-1, 2-7, 7-8, 8-9, 9-7, 4-10, 10-11. Vertices are coloured by the parity of their…