Find an element in an infinite length sorted array

后端 未结 7 1127
北海茫月
北海茫月 2020-12-13 21:51

Given an infinite length sorted array having both positive and negative integers. Find an element in it.

EDIT
All the elements in the array are

7条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-13 22:24

    Just my 2 cents. We have an infinite array thus lets imagine that we are looking for very big number. Did you imagine? Well it's ever much more bigger. Note that length of interval to binary search in is 2^i = 2^(i+1)-2^i thus it should take log(2^i)=i time to find the number. On the other hand it takes i time to reach the target interval. So the total time complexity is O(n) again. What I'm missing?

提交回复
热议问题