GATE Computer ScienceHardGATE CS (PYQ pattern)
The Z-array of a string s has Z[i] = length of the longest common prefix of s and s[i..]. (Z[0] is taken as 0.) What is the sum of all Z values of s = aaabaaaab?
- A. 11
- B. 15
- C. 13
- D. 17
Correct answer: C. 13
Z = [0, 2, 1, 0, 3, 4, 2, 1, 0], and the sum is 13. For example Z[4] = 3 because aaaab matches the prefix aaab for three characters and then fails, while Z[5] = 4 because s[5..] = aaab equals the prefix aaab.
More Algorithms questions
- What is the length of the longest palindromic substring (contiguous) of xabcbaxyzzyxabcdcbay?
- Consider the congruence 46x ≡ 18 (mod 240). How many solutions x does it have in 0…239, and what is the smallest one?
- How many integers in 1…1000 are coprime to 360?
- A dynamic array has capacity 1 initially. An append to a full array doubles the capacity (copying all n current elements); after a…
- A 4-input comparator network applies, in order, the comparators (0,1), (2,3), (0,2), (1,3), (1,2), each putting the smaller value on the…
- How many comparators does Batcher's bitonic sorting network for 16 inputs use?