0

Given an N sorted elements in the beginning of an infinite array, find if a given k elements exists in the array in O(logN) time, or return proper message if element does not exist.

What i have come up with so far is a modification of binary search since we dont know the length of array(we know its infinite but we dont know N) i use this:

if k > A[i] then let i = i*2
If k == A[i], then we re done, otherwise we do binary search as usual on A[1..i].

The only problem is that with this pseudocode i am afraid the time it will take to make the search will be higher than O(logN) since there is a chance that we will run multiple binary searches so its gonna be something of O(klogN).

Any ideas??

mario vergo
  • 49
  • 2
  • 4
  • Your question is not clear. – Daniel Daranas Mar 26 '15 at 15:54
  • Given an array of unknown size, of N (unknown) sorted elements(lets say from high to low) can you find if a k elements exists in the array in O(logN) using binary search ?? Or in general you can use a Divide & Conquer technique. – mario vergo Mar 26 '15 at 15:58
  • 1
    Can we assume that, after the N sorted elements, the array contains only zeros? (Or some other value easily distinguished from the N sorted elements; perhaps MAXINT)? If so, then your approach should work with a few tweaks. As long as you only run some constant number of binary searches -- I suspect there is a way to do it with at most 4 binary searches -- then the total run time is O(4 log N) or similar which is equivalent to O(log N), in the same way that O( 42 ) is equivalent to O(1). – David Cary Mar 26 '15 at 16:17

0 Answers0