Define N = 2 n
. The array contains N
elements.
Define M
as the number of times maj
appears in the array. The definition of “majority element” is that M > N/2
.
Now divide the array into pairs p[1] ... p[n]
. Define q0
as the number of pairs that contain zero instances of maj
. Define q1
as the number of pairs that contain exactly one instance of maj
. Define q2
as the number of pairs that contain exactly two instances of maj
.
Then N = 2 q0 + 2 q1 + 2 q2
and M = q1 + 2 q2
.
Substitute into the inequality defining the majority element, and simplify:
M > N / 2
q1 + 2 q2 > (2 q0 + 2 q1 + 2 q2) / 2
q1 + 2 q2 > q0 + q1 + q2
2 q2 > q0 + q2
q2 > q0
So the number of pairs containing two instances of maj
exceeds the number of pairs containing zero instances of maj
.
Now define M'
to be the number of times maj
appears in the new array, after running your algorithm. The algorithm deletes one maj
for each q1
pair, and one maj
for each q2
pair. So M' = M - q1 - q2
.
Define N'
to be the size of the new array produced by the algorithm. The algorithm deletes two elements for each q1
pair, and one element for each q2
pair.
But we don't know how many elements the algorithm deletes for each q0
pair. Some of the q0
pairs contain two different elements, and the algorithm deletes both. But the other q0
pairs contain identical (non-maj
) elements, and the algorithm deletes only one.
One extreme is that all of the q0
pairs are deleted entirely. In that case, the algorithm deletes 2 q0
elements, so
N - 2 q1 - q2 - 2 q0 ≤ N'
The other extreme is that only one element is deleted from each q0
pair. In that case, the algorithm deletes q0
elements, so
N - 2 q1 - q2 - q0 ≥ N'
Let's go back to the definition of “majority element” and do some algebra:
M > N / 2
M - q1 - q2 > N / 2 - q1 - q2
M - q1 - q2 > (N - 2 q1 - 2 q2) / 2
M - q1 - q2 > (N - 2 q1 - q2 - q2) / 2
The left-hand side is M'
.
M' > (N - 2 q1 - q2 - q2) / 2
Can we turn the right-hand side into N' / 2
? First, multiply both sides by 2:
2 M' > N - 2 q1 - q2 - q2
Recall that we proved q2 > q0
. Therefore
2 M' > N - 2 q1 - q2 - q2 > N - 2 q1 - q2 - q0
and, since we deduced N - 2 q1 - q2 - q0 ≥ N'
,
2 M' > N - 2 q1 - q2 - q0 ≥ N'
so
2 M' > N'
M' > N' / 2
Thus maj
appears sufficient times in the new array to be the majority element of the new array. QED.