Question
Let 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 such that is 1 by probing the minimum number of locations in . The worst case number of probes performed by an optimal algorithm is ______.
Official answer and solution
- Official answer
- 5
- From
- Official GATE 2017 answer key
Solution
Formulate the search as finding a split point in a sorted binary array of length 31 using binary search / decision trees.
The array of 31 elements consists of a sequence of 0's followed by a sequence of 1's. The first index where can be any integer from 1 to 31, or the array may contain no 1's at all (all zeros).
This means there are possible outcomes for the position of the boundary.
Each probe of an array element returns a binary answer (0 or 1), splitting the remaining search space into two parts. In terms of decision trees, an algorithm with possible outcomes requires a decision tree with at least leaves. The worst-case number of probes is the height of the decision tree, which is bounded by:
Standard binary search achieves this theoretical lower bound:
- Probe 1 leaves candidates.
- Probe 2 leaves candidates.
- Probe 3 leaves candidates.
- Probe 4 leaves candidates.
- Probe 5 identifies the exact index uniquely.
Hence, the worst-case number of probes is 5.
5.0
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2017 Computer Science and Information Technology (CS) Session 1
- Question
- 48 of 65, printed as CS Q.48
- Section
- Computer Science and Information Technology
Report an error in this question
GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.