How to find the interval with max number of intersections? Like if (1,3),(2,9),(4,7),(5,8), output should be (2,9).
I believe one common approach is to create an array = {(a1,start),(b1,end),(a2,start),(b2,end),...,(an,start),(bn,end)} (the first coordinate is a number and the second coordinate is a label). Sort the array according to lexicographic order where start < end (the first coordinate is a number). Initialize count=0 (count will mark the current number of overlapping intervals). Increase the count when sees a start coordinate and decrease the count when sees an end coordinate.
What I don't understand when we create the array, we are essentially separating the start time and end time from each interval. So each start time and end time will lose their identify of which interval they belongs to. When we walk through the array, it would not make sense if we just increment/decrement the count without knowing each coordinate corresponds to which interval. If we use a hashmap and map each coordinate to their interval, there might be issues with duplicate start times and end times.
How should we solve this issue? Thanks.