Suppose we have a list of intervals with positive integer endpoints, ranging from 1 to n. We wish to find i such that i is contained the most number of intervals. For example,
Intervals
(1,4)
(4,10)
(7,9)
(2,4)and n=10
Then the frequencies for each number is
1 - 1
2 - 2
3- 2
4- 3
5- 1
6- 1
7- 2
8- 2
9- 2
10-1
Thus the mode is 4. If there are multiple modes, we select the smallest one.
The simple approach would be to have an array of size n and update each time an interval contains a number which is probably not optimal. I tried viewing this my first sorting the intervals by first starting endpoint and then ending endpoint but got nothing better. Any optimizations?