Suppose
J
1
= [
x
1
,
y
1
]
is any interval of the left half and
J
2
= [
x
2
,
y
2
]
is any interval of the right half. Could
this be a larger overlap than anything found by
F
? We’ll prove that it cannot. Since
J
2
is an interval of
the right half, its left endpoint is at least
x
, i.e.,
x
2
≥
x
.
Therefore the intersection
J
1
∩
J
2
is contained
in
[
x
,
∞
)
, so
J
1
∩
J
2
= [
x
,
y
1
]
∩
J
2
.
Let
J
= [
x
0
,
y
0
]
be the interval found in step 8.
Similarly, we’ll have
J
∩
J
2
= [
x
,
y
0
]
∩
J
2
. Now due to the way
J
was selected in step 8, the right endpoint of
J
is at least as large as
the right endpoint of
J
1
, i.e.,
y
0
≥
y
1
. This means that
[
x
,
y
1
]
⊆
[
x
,
y
0
]
. It follows that
[
x
,
y
1
]
∩
J
2
⊆
[
x
,
y
0
]
∩
J
2
,
i.e.,
J
1
∩
J
2
⊆
J
∩
J
2
, i.e., overlap
(
J
1
,
J
2
)
≤
overlap
(
J
,
J
2
)
. Now given the way the loop in steps 9–10 works,
we see that
O
≥
overlap
(
J
,
J
2
)
, so
O
≥
overlap
(
J
1
,
J
2
)
. Therefore
J
1
,
J
2
cannot have higher overlap than
what was returned by
F
. This means that
F
correctly finds the largest possible overlap between any pair of
intervals.
Alternate proof of the last paragraph: Suppose
J
1
is an interval of the left half and
J
2
is an interval of the
right half. Then the left endpoint of
J
2
is at least
x
. Therefore their intersection lies in
[
x
,
∞
)
. The left
endpoint of
J
1
is at most
x
, therefore it does not affect the size of the overlap. In other words, we can replace
the left endpoint of
J
1
with
x
and nothing changes. Now if we hypothetically assume all left endpoints of
CS 170, Fall 2014, Sol 2
10

the left intervals are
x
, it is obvious that the best we can do is find the one that has the highest right endpoint
(regardless of the choice of
J
2
we end up with the highest overlap). Line 9 finds exactly this interval, and
then we check its overlap with all intervals of the right half. Therefore we must have considered at least one
of the pairs from the left half and the right half that have the highest overlap.
Comment about pseudocode:
Notice that we didn’t bother spelling out how you find the interval whose
right endpoint is maximal (step 8), how to compute the overlap of two intervals (step 10), or other low-level
implementation details. Try to follow this practice in your own solutions, too: don’t drown the reader in low-
level details, write your pseudocode at a high level of abstraction so that it is as easy to read and understand
as possible.
CS 170, Fall 2014, Sol 2
11