Imagine I have a value: x, and a value range: y to z (assume y < z). Determining if x falls within y to z is simple:
if(x >= y && x <=z)
true
else
false
Now imagine that I, again, have my value x, but I have a list of ranges: [y1 to z1, y2 to z2, ...].
Note that there are no constraints between elements in the list. Ranges can overlap. One range can fall entirely within another range. Etc.
I want to answer the question does x fall with the range of any of these ranges?. I could, of course, iterate through the list and check each range, one at a time, using the method described above. But I want to know if there is any optimization I could do.
Ideally I was hoping there might be a constant time solution, perhaps by sorting the ranges somehow and doing something clever. But it's just not coming to me.
Short of a constant time solution I would accept an answer that presents an optimization along with an explanation of why that is the best O(n) time achievable. Or if iteration through the list really is the best you can get, I would also accept answer explaining why that is the case.
Note that I do not need to find all the ranges that x falls into. I just need to know, true or false - does x fall within at least one of the ranges.