1

I know a similar question exists here. My question is also same that I have N intervals (some possibly overlapping some even same). Then Q point queries are given and I need to tell how many intervals contains this point.

I tried to develop my algorithm by sorting the end point array then counting the number of overlapped interval by +1, -1 trick as mentioned in an answer. But after performing the binary search what I should do? Because its not always the case that the corresponding index of the prefix sum array is the answer.

e.g. 
Intervals are : [1,4] [5,7] [6,10] [7,13]
sorted end point array : [1,4,5,6,7,7,10,13]
+1/-1 array : [1,-1,1,1,1,-1,-1,-1]
prefix sum array : [1,0,1,2,3,2,1,0]

Query : 10
my algorithm gives 1 (corresponding prefix array)
but actual ans should be 2.

How should I fix my algorithm?

Sujan Dutta
  • 161
  • 8

2 Answers2

3

There are no good answers in the question you linked, so:

First:

  1. Put the entry and exit positions of each interval into separate arrays. (if you are using closed intervals then exit position is end position + 1, i.e., in [4,6], entry is 4, exit is 7.
  2. Sort the arrays.

Then, for each point p:

  1. Binary search in the entry array to find the number of entry positions <= p.
  2. Binary search in the exit array to find the number of exit positions <= p.
  3. The number of intervals that contain the point is entry_count - exit_count

NOTE that the number of positions <= p is the index of the first element > p. See: Where is the mistake in my code to perform Binary Search? to help you get that search right.

For your example:

Intervals: [1,4], [5,7], [6,10], [7,13]
Entry positions: [1,5,6,7]
Exit positions: [5,8,11,14]
Entry positions <= 6:  3
Exit positions <= 6: 1
Intervals that contains 6:  3-1 = 2
Matt Timmermans
  • 53,709
  • 3
  • 46
  • 87
  • I like this solution more, just need to be careful when doing the binary search when having multiple start or end points at same place. – juvian Oct 10 '18 at 21:42
1

Problem is your intervals are [] instead of [), and the answer probably was made for the latter . First transform your end indendexes to value -1.

After this + "compressing" repeated coordinates you should have:

 points = [1,5,6,7,8,11,14]
 sums = [1,0,1,1,-1,-1,-1]
 accumulated = [1,1,2,3,2,1,0]

Then for a query, if query < points[0] or query > points[max] return 0. If not, binary search over points to get index and the answer lies on accumulated[index].

juvian
  • 15,875
  • 2
  • 37
  • 38