in reply to Computing array intersections
Does anyone know how to pre-processes N to make those queries in O(1) time because this would be ideal.
Not possible. At least not without infinite memory capacity.
About the best you could do is create a hash or bit-vector from the larger of the two intervals suitably adjusted (+/-Z), so that you can do O(1) lookups of the values in the smaller interval, within that.
If you can arrange to deal with all the Z=x in one pass and Z=y in the next and so on, then you could build a suitably adjusted array the same size as the original from which you can build your lookup hashes/vectors for each new range pair, but its unlikely to make a huge difference.
The lookup removes one level of nested loop from the problem, but I see no way of reducing the others.
|
|---|