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
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.
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)