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

前端 未结 7 1600
名媛妹妹
名媛妹妹 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.

提交回复
热议问题