Introduction to Algorithms
October 6, 2006
Massachusetts Institute of Technology
6.046J/18.410J
Professors Erik Demaine and Madhu Sudan
Problem Set 3 Solutions
Problem Set 3 Solutions
Problem 31.
Popular Elements
Given an array
A
[1
. . n
]
of
n
elements, and given a constant
0
< α <
1
, we say that an element
x
is
α
popular
in
A
if
x
appears more than
αn
times in
A
. Our goal is to ﬁnd an algorithm that
reports all
α
popular elements in
A
, i.e., that computes the set
B
α
=
±
x
:
{
i
:
A
[
i
] =
x
}
> αn
²
.
Our goal is to obtain a running time of
O
(
n
lg(1
/α
))
.
(a)
How many
α
popular elements can there be, i.e., how large can

B
α

be?
Solution:
If
A
has
n
elements, we must have less than
1
/α
elements that appear more
than
αn
times. Thus,

B
α

<
1
/α
.
(b)
Give a deterministic
O
(
n
)
time algorithm for computing all
α
popular elements when
α
= 1
/
2
. Your algorithm should call S
ELECT
O
(1)
times. Prove that your algorithm
reports the correct answer.
Solution:
By part (a),

B
1
/
2

<
2
, i.e.,

B
1
/
2
 ≤
1
. Thus, there can be at most one
1
/
2

popular element
x
. If such an
x
exists, we will show that
x
must be the median. The
algorithm is to compute the median element
y
of
A
, and then scan
A
to count whether
y
appears more than
n/
2
times. Computing the median element
y
and scanning the
array both take
O
(
n
)
time.
Suppose for contradiction that
x
is
1
/
2
popular, but
y
6
=
x
is the median element.
Suppose
y < x
; the case
y > x
is symmetric. If
n
is odd, then there are at least
(
n
+ 1)
/
2
elements greater than
y
in
A
. If
n
is even, then there are least
n/
2 + 1
elements greater than
y
in
A
. Both contradict the fact that
y
is the median.
(c)
Generalize your algorithm from part (b) to ﬁnd an
O
(
n/α
)
time algorithm to compute
B
α
for any constant
α
. Your algorithm should call S
ELECT
O
(1
/α
)
times.
Solution:
Let
s
i
=
i
d
αn
e
, for
i
= 1
,
2
, . . .
b
n/
d
αn
ec
, and let
t
i
be the element in
A
with rank
s
i
. If any object
x
appears more than
αn
times in the list, then we must have
x
=
t
i
for some
i
.
The proof of this fact is a generalization of the argument from part (b). Let
A
0
be
the sorted version of
A
. Then any element
x
which is
α
popular must appear in a