Question

Let AA 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 ii such that A[i]A[i] is 1 by probing the minimum number of locations in AA. 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 AA of 31 elements consists of a sequence of 0's followed by a sequence of 1's. The first index ii where A[i]=1A[i] = 1 can be any integer from 1 to 31, or the array may contain no 1's at all (all zeros).

This means there are 31+1=3231 + 1 = 32 possible outcomes for the position of the boundary.

Each probe of an array element A[k]A[k] returns a binary answer (0 or 1), splitting the remaining search space into two parts. In terms of decision trees, an algorithm with KK possible outcomes requires a decision tree with at least KK leaves. The worst-case number of probes is the height of the decision tree, which is bounded by:

Worst-case probes≥⌈log⁡2K⌉=⌈log⁡232⌉=5\text{Worst-case probes} \ge \lceil \log_2 K \rceil = \lceil \log_2 32 \rceil = 5

Standard binary search achieves this theoretical lower bound:

  • Probe 1 leaves ≤16\le 16 candidates.
  • Probe 2 leaves ≤8\le 8 candidates.
  • Probe 3 leaves ≤4\le 4 candidates.
  • Probe 4 leaves ≤2\le 2 candidates.
  • Probe 5 identifies the exact index uniquely.
    Hence, the worst-case number of probes is 5.
Final answer

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.