3

I've got this exercise where I can't figure out what I'm missing. It's actually very similar to an existing question, but not the same and the solution is probably not the same. The existing question: Finding the most frequent number from a set of ranges -

The problem:


You are given n ranges [ai, bi] in the range of [0, nd], where d is a constant positive integer. ai, bi are also integers, and 0 <= ai <= bi <= nd for each i (i = 1, ..., n).

Write an algorithm to find the integer z that appears in the greatest amount of ranges [ai, bi]. The complexity must be linear (as a function of n, d).


It seems to me like a classic counting/radix sort problem: use k = n as the radix and the complexity of the sort becomes O(d*n). The question is, what to do next? I'm a bit stuck on this. In the related question, there is an assumption that the range can only be a certain size, while in my question there is no such assumption, and theoretically there can be a range of [0, nd] and anything in between.

Random example:

Input: [1, 3], [5, 10], [5, 11], [6, 17], [8, 9], [9, 21], [11, 15], [12, 12]
Output: 9
Community
  • 1
  • 1
Ynhockey
  • 3,845
  • 5
  • 33
  • 51

1 Answers1

2

Sorting is a good idea (radix sort seems to be the way to go), but I wouldn't sort the intervals. Instead, I would use the sweep line style approach. Imagine the intervals being drawn on a number line from 0 to n^d. Now we "sweep" from left to right.

The interesting points were the situation of how many intervals intersect our current position can change, are the start/end points of our intervals. Note that we can always choose one of the start or end points as the solution as well.

So we just consider those points and sweep from left to right:

events = start points UNION end points
sort events (rank start points before end points in case of a tie)
count = 0
max = 0
for each event x in sorted order
    if x is start point
        count++
    if x is end point
        count--
    if count > max
        max = count
        result = x
Niklas B.
  • 92,950
  • 18
  • 194
  • 224
  • +1. (I see you just edited this to ensure that endpoints compare greater than "equal" starting points :)) Also it would help to be specific that the sort is a radix sort with radix n, as suggested by the OP. – j_random_hacker Mar 22 '14 at 23:20
  • @j_random_hacker Yes, I think the sorting is not OPs problem because he already had that idea, but I added it to the question – Niklas B. Mar 22 '14 at 23:24
  • 1
    Thank you for the answer! I am trying to understand something though: does the most frequent number (z) have to correspond to one of the events? I'm trying to rack up my brain for a counterexample (and can't find one), but intuitively it seems like there should be possible cases where the most frequent number never appears as either a starting or an ending point. – Ynhockey Mar 22 '14 at 23:27
  • 1
    @Ynhockey Let's assume we have a solution z that is not an end point. Go to the nearest event point to the left or right from z. There you will have the exact same amount of intervals (because the set of intervals only changes at event points) :) So while not *all* possible solutions are end points, there is always an end point that is a valid solution – Niklas B. Mar 22 '14 at 23:30