GATE Computer ScienceVery hardGATE CS (PYQ pattern)
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 decrement otherwise) is run on [3, 3, 4, 2, 4, 4, 2, 4, 3, 3, 4, 3, 3, 3, 4, 2]. Which statement is correct?
- A. The returned candidate is 3, but 3 is not a majority element
- B. The returned candidate is 4, which is a majority element
- C. The returned candidate is 3, and it is a majority element
- D. The algorithm returns 2, which is not a majority element
Correct answer: A. The returned candidate is 3, but 3 is not a majority element
Tracing gives candidate 3 at the end with counter 0. The array has 16 elements and 3 occurs only 7 times (4 occurs 6 times, 2 occurs 3 times), so no element occurs more than 8 times: the candidate must be verified in a second pass.
More Algorithms questions
- 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…
- Assume P ≠ NP. Which of the following problems have a polynomial-time algorithm? I. 2-SAT. II. 3-colourability of a graph. III. Maximum…