Find an element that occurs at least k times in a sorted array in log(n) time

前端 未结 2 1217
我寻月下人不归
我寻月下人不归 2021-01-07 11:17

Given a sorted array of n elements and a number k, is it possible to find an element that occurs more than k times, in log(n) time? If there is more than one number that occ

相关标签:
2条回答
  • 2021-01-07 11:58

    Not in general. For example, if k=2, no algorithm that doesn't in the worst case inspect every element of the array can guarantee to find a duplicate.

    0 讨论(0)
  • 2021-01-07 12:12

    Here is O(n/k log(k)) solution:

    i = 0
    while i+k-1 < n: //don't get out of bounds
       if arr[i] == arr[i+k-1]:
           produce arr[i] as dupe
           i = min { j | arr[j] > arr[i] } //binary search
       else:
           c = min { j | arr[j] == arr[i+k-1] } //binary search
           i = c
    

    The idea is, you check the element at index i+k-1, if it matches the element at index i - good, it's a dupe. Otherwise, you don't need to check all the element between i to i+k-1, only the one with the same value as arr[i+k-1].

    You do need to look back for for the earliest index of this element, but you are guaranteed to exceed the index i+k by next iteration, making the total number of iteration of this algorithm O(n/k), each takes O(logk) time.

    This is asymptotically better than linear time algorithm, especially for large values of k (where the algorithm decays to O(logn) for cases where k is in O(n), like for example - find element that repeats at least with frequency 0.1)

    0 讨论(0)
自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题