Let be an array of numbers consisting of a sequence of 's followed by a sequence of 's. The problem is to find the smallest index such that is by probing the minimum number of locations in . The worst case number of probes performed by an optimal algorithm is ____________.
View worked solution
Answer: 5
The problem asks for the minimum number of probes in the worst case to find the first transition from to in a sorted array of size . This can be modeled as a binary search problem.
Method 1: Decision Tree / Information Theory
Let the array indices be . The first occurrence of can be at any of these indices, or there might be no at all in the array. This gives us a total of possible outcomes.
Each probe at an index yields a binary outcome: either or . Therefore, any decision tree representing an optimal search algorithm must have at least leaves.
The minimum height (worst-case number of probes) of a binary decision tree with leaves is given by:
Method 2: Binary Search Execution
We can find the transition index using standard binary search over the range :
- Probe 1: Query the middle element at index .
- If , the first lies in the range (size ).
- If , the first lies in the range (size ).
- Probe 2: Halves the search space to size .
- Probe 3: Halves the search space to size .
- Probe 4: Halves the search space to size .
- Probe 5: Halves the search space to size , uniquely identifying the transition index.
Thus, the worst-case number of probes performed by an optimal algorithm is 5.
Free practice · Your answers on this page are not saved.
