Solution: Sorted Array Multiple Search

Solution for the Sorted Array Multiple Search Problem.

We'll cover the following


To solve the Multiple Search Problem, let’s first look at the Binary Search Problem to search a single key in a sorted array of keys.

\rule[0 pt]{1000 pt}{0. pt}

Sorted Array Search Problem

Search a key in a sorted array of keys.

Input: A sorted array KK =[k0,,kn1]= [k_{0},\ldots,k_{n-1}] of distinct integers (i.e.,k0<k1<<kn1k_{0} < k_{1} <\ldots< k_{n-1}) and an integer qq.

Output: Check whether qq occurs in KK.

\rule[0 pt]{1000 pt}{0. pt}

A naive way to solve this problem is to scan the array KK (running time O(n)O(n)). The BinarySearchBinarySearch algorithm below solves the problem in O(log(n))O(log (n)) time. It is initialized by setting minIndexminIndex equal to 00 and maxIndexmaxIndex equal to n1n−1. It sets midIndexmidIndex to (minIndex+maxIndex)/2(minIndex + maxIndex)/2 and then checks to see whether qq is greater than or less than KK[midIndexmidIndex]. If qq is larger than this value, then BinarySearchBinarySearch iterates on the subarray of KK from minIndexminIndex to midIndex1midIndex − 1; otherwise, it iterates on the subarray of KK from midIndex+1midIndex + 1 to maxIndexmaxIndex. The iteration eventually identifies whether qq occurs in KK.

 BinarySearch(K[0..n1],q)BinarySearch(K[0..n − 1], q)
 minIndexminIndex \leftarrow 0
 maxIndexmaxIndex \leftarrow n1n−1
 while maxIndexminIndexmaxIndex ≥ minIndex:
  midIndexmidIndex \leftarrow ⌊(minIndex+maxIndexminIndex + maxIndex)/ 2⌋
  if KK[midIndexmidIndex] == qq:
   return midIndexmidIndex
  else if KK[midIndexmidIndex] << q:
   minIndexminIndex \leftarrow midIndex+1midIndex + 1
   maxIndexmaxIndex \leftarrow midIndex1midIndex − 1
 return “key not found”

For example, if q=9q = 9 and K=[1,3,7,8,9,12,15]K = [1, 3, 7, 8, 9, 12, 15], BinarySearchBinarySearch would first set minIndex=0minIndex = 0, maxIndex=6maxIndex = 6, and midIndex=3midIndex = 3. Since qq is greater than KK[midIndexmidIndex] =8= 8, we examine the subarray whose elements are greater than KK[midIndexmidIndex] by setting minIndex=4minIndex = 4, so that midIndexmidIndex is recomputed as (4+6)/2=5(4 + 6)/ 2 = 5. This time, qq is smaller than KK [midIndexmidIndex] =12= 12, and so we examine the subarray whose elements are smaller than this value. This subarray consists of a single element, which is qq.

The running time of BinarySearchBinarySearch is O(log(n))O(log(n)) since it reduces the length of the subarray by at least a factor of 22 at each iteration of the while loop. Note however that our grading system is unable to check whether you implemented a fast O(log(n))O(log(n)) algorithm for the sorted array search or a naive O(n)O(n) algorithm. The reason is that any program needs a linear time in order to just read the input data. For this reason, we asked you to solve the multiple search problem.


Here is an implementation of the challenge. Hit the “Run” button and observe the output.

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