We have two databases of size n containing numbers without repeats. So, in total we have 2n elements. They can be accessed through a query to one database at a time. The query is such that you give it a k and it returns kth smallest entry in that database. We need to find nth smallest entry among all the 2n elements in O(log n) queries.
The idea is to use divide and conquer but I need some help thinking through this. Thanks!
I saw this problem on a qualifying exam not long ago, and it smells like a homework problem. I will therefore make only two suggestions:
Study binary search, with careful attention to the precise nature of the loop invariants. Jon Bentley's book Programming Pearls has a good explanation
Try generalizing the invariants for binary search.
Draw a picture with various special cases:
This is a fairly hard problem; you'll have an easier time if you go straight for a proof of correctness.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With