Let A be an array of 31 numbers consisting of a sequence of 0’s followed by a…
2021
Let A be an array of 31 numbers consisting of a sequence of 0’s followed by a sequence of 1’s. The problem is to find the smallest index i such that A[i] is 1 by probing the minimum number of locations in A. The worst-case number of probes performed by an optimal algorithm is
- A.
8
- B.
4
- C.
5
- D.
10
- E.
Question not attempted
Attempted by 674 students.
Show answer & explanation
Correct answer: C
To find the smallest index i such that A[i] is 1 in an array of 31 numbers (0s followed by 1s), we can use binary search. The goal is to minimize the number of probes in the worst case. Binary search works by repeatedly dividing the search interval in half. The worst-case number of probes is determined by the number of times we can divide 31 by 2 until we reach 1. This is equivalent to finding the smallest integer k such that 2^k ≥ 31. Since 2^4 = 16 < 31 and 2^5 = 32 ≥ 31, the answer is 5. Thus, the worst-case number of probes performed by an optimal algorithm is 5.