Given lots of intervals [ai, bi], find the interval that intersects the most number of intervals. Can we do this in O(nlogn) or better? I can think only of a n^2 approach.
Suppose the intervals are given as (a1,b1), ..., (an,bn)
. Make a sorted array of length 2n
where ties are broken by
ai = aj
, then put ai
first iff bi < bj
bi = bj
, then put bi
first iff ai < aj
ai = bj
, then put ai
firstMark each point as an a
or a b
(maybe keep a binary array of length 2n
to do this). Walk through the array(s) keeping track of how many intervals touch the given point (running total of a
s minus running total of b
s). The maximum number encountered happens at the interval with the most overlap.
This is O(n log n)
due to the sorting of the intervals.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With