A list of $k$ elements, possibly with repeats, is given. The goal is to find if there is a majority element. This is defined to be an element $x$ such that the number of times $x$ occurs in the list is strictly greater than $\frac{k}{2}$. (Note that there need not be such an element, but if it is there, it must be unique.) A celebrated efficient way to do this task uses two functions $f$ and $m$ with domain $\{1, 2, \ldots, k\}$. The functions are defined inductively as follows.
Define $f(1) =$ first element of the list, $m(1) = 1$. Assuming $f$ and $m$ are defined for all inputs from 1 to $i$, define $$f(i+1) = \begin{cases} f(i) & \text{if } m(i) > 0 \\ (i+1)^{\text{th}} \text{ element of the list} & \text{if } m(i) = 0 \end{cases}$$ $$m(i+1) = \begin{cases} m(i) - 1 & \text{if } m(i) > 0 \text{ and } (i+1)^{\text{th}} \text{ element of the list is other than } f(i) \\ m(i) + 1 & \text{otherwise} \end{cases}$$
(a) For the example of length 15 given below, write a sequence of 15 letters showing the values of $f(i)$ and a sequence of 15 numbers directly underneath showing the values of $m(i)$ for $i = 1, 2, \ldots, 15$.
$$\text{a a b a b c c b b b a b b c b}$$
(b) Prove that in general the list can be divided into two disjoint parts A and B such that
- Part A contains $m(k)$ elements of the list each of which is $f(k)$.
- Part B contains the remaining $k - m(k)$ elements of the list and B can be written as disjoint union of pairs such that the two elements in each pair are distinct.
(c) If there is a majority element, show that it must be $f(k)$. You may assume part (b) even if you did not do it.
(d) Assuming $f(k)$ is the majority element, answer the following two questions. Show by examples that the number of occurrences of $f(k)$ in the list does not determine the value of $m(k)$. Can the value of $m(k)$ be anything in $\{0, \ldots, k\}$? Find constraints if any on the possible values of $m(k)$.
(e) Now assume instead that an element occurs exactly $\frac{k}{2}$ times in the list. Is it necessary that $f(k)$ is such an element?