I have two buckets (unordered, 1-dimentional data structures) of numbers and I want to calculate a minimal distance between any elements of the two buckets. Is there a way to find the shortest distance between any number from different buckets in O(1)
? What is my best bet?
Input
[B1] 1, 5, 2, 347, 50
[B2] 21, 17, 345
Output
2 // abs(347 - 345)
Edits
- I expect to have more lookups than inserts
- Distance between smallest and largest elements in any bucket is less than 10^5
- Number of elements in any bucket is less than 10^5
- Numbers in buckets are "nearly" sorted - these are timestamps of events. There's probably less than 1% of elements in the buckets that are out of order
- The number of elements in buckets is small, but I need to lookup at an average rate of 2k/sec, and periodically drop stale buckets and replace them with new buckets, hence I want my lookups be in
O(1)
See why I need this and what I have thought of in the previous question edition.