GATE Computer ScienceVery hardGATE CS (PYQ pattern)
For the string s = abaababaabaab (13 characters), a number p (1 ≤ p ≤ 13) is called a period if s[i] = s[i + p] for every valid i (p = 13 counts trivially). How many periods does s have?
- A. 2
- B. 1
- C. 3
- D. 4
Correct answer: C. 3
Periods correspond to borders: p is a period iff s has a border of length 13 − p. The borders of s are abaab (5), ab (2) and the empty string (follow the KMP failure chain 5 → 2 → 0), so the periods are 8, 11 and 13. Forgetting the trivial period gives 2; taking only n − π[n−1] = 8 gives 1.
More Algorithms questions
- Boyer–Moore majority vote (candidate c and counter k; if k = 0 take the current element as c with k = 1; else increment k when equal and…
- In the Josephus problem 30 people numbered 1 to 30 stand in a circle; counting starts at person 1 and every third person is eliminated (3…
- Quicksort partitions m elements with exactly m − 1 comparisons and then recurses on the two sides (sizes k and m − 1 − k). Over all inputs…
- Keys 50, 30, 70, 20, 40, 60, 80 are inserted in this order into an empty binary search tree. How many of the 7! insertion orders of these…
- Floyd's cycle-finding is applied to the sequence x₀ = 2, x{i+1} = (xi² + 1) mod 55. How many distinct values does the sequence take?
- Bellman–Ford is run from A on the directed graph A→B 4, A→C 2, C→B 1, B→D 5, C→D 8, D→E 3, C→E 10, E→F −2, D→F 6. In each pass the edges…