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?

  1. A. The returned candidate is 3, but 3 is not a majority element
  2. B. The returned candidate is 4, which is a majority element
  3. C. The returned candidate is 3, and it is a majority element
  4. 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

Exams that ask this topic