In less-than-linear time, find the duplicate in a sorted array

前端 未结 7 1464
名媛妹妹
名媛妹妹 2020-12-31 11:00

Today, an interviewer asked me this question. My immediate response was that we could simply do a linear search, comparing the current element with the previous element in t

相关标签:
7条回答
  • 2020-12-31 11:21

    Can be done in O(log N) with a modified binary search:

    Start in the middle of the array: If array[idx] < idx the duplicate is to the left, otherwise to the right. Rinse and repeat.

    0 讨论(0)
提交回复
热议问题