Solution: Multiple Search with Duplicates

Solution for the Binary Search with Duplicates Problem.

We'll cover the following

Solution

Given a key qq, our goal is to find the first (top) occurrence of this key in the array KK. For example, if K={3,6,6,7,7,7,7,9}K = \{3,6,6,7,7,7,7,9\} and the key qq is 77, then the first occurrence of this key is located at index 33. Of course, we can find one of the occurrences of the key by simply launching the binary search. Afterward, we can find the first occurrence of the key by consecutively checking the element before the position of the found element, as illustrated in the pseudocode below.


 NaiveBinarySearchWithDuplicates(K[0n1],q)NaiveBinarySearchWithDuplicates(K [0\dots n − 1], q):
 minIndex0minIndex ← 0
 maxIndexn1maxIndex←n−1
 while maxIndexminIndexmaxIndex ≥ minIndex:
midIndex(minIndex+maxIndex)/2midIndex \leftarrow ⌊(minIndex + maxIndex)/ 2⌋
 if K[midIndex]=qK[midIndex] = q:
  topmidIndextop ← midIndex
  while top>0top > 0 and K[top1]=K[top]K[top − 1] = K[top]:
    toptop1top←top−1
  return toptop
 else if K[midIndex]<qK[midIndex] < q:
  minIndexmidIndex+1minIndex ← midIndex + 1
 else:
  maxIndexmidIndex1maxIndex ← midIndex − 1
return 1−1


Stop and think: What is the running time of this algorithm?

This algorithm might become slow in the case of an array with many duplicates. For example, if a single duplicated element accounts for half of an array, NaiveBinarySearchWithDuplicatesNaiveBinarySearchWithDuplicates takes linear O(n)O(n) time instead of logarithmic O(logn)O(log n) time. This problem is fixed in the pseudocode below.


 BinarySearchWithDuplicates(K[0..n1],q)BinarySearchWithDuplicates(K [0..n − 1], q):
 minIndex0minIndex ← 0
 maxIndexn1maxIndex←n−1
 result1result ← −1
 while maxIndexminIndexmaxIndex ≥ minIndex:
midIndex(minIndex+maxIndex)/2midIndex ← ⌊(minIndex + maxIndex)/ 2⌋
 if K[midIndex]=qK[midIndex] = q:
  maxIndexmidIndex1maxIndex ← midIndex − 1
  resultmidIndexresult ← midIndex
 else if K[midIndex]<qK[midIndex] < q:
  minIndexmidIndex+1minIndex ← midIndex + 1
 else:
  maxIndexmidIndex1maxIndex ← midIndex − 1
return resultresult


Code

Here is the implementation of the algorithm above.

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.