Searching, Sorting & Selection
![Question 48, GATE 2017: An array of 31 entries consists of zeros followed by ones. Find the smallest index i with A[i]=1 using the minimum number of array probes. What is an optimal algorithm’s worst-case probe count?](../../assets/questions/CS-2017-S1-Q58-1.webp)
Answer & solution
Official answer: 5.0
Our answer: 5
1. A probe returns one bit: if A[mid]=0 the first one lies to the right; if A[mid]=1 it lies at mid or to the left. This monotonicity permits binary search.
2. Balanced probing halves the candidate boundary positions each time. Five probes suffice for 31 first-one positions, or 32 possibilities if an all-zero sentinel is allowed.
3. For optimality, a binary decision tree of depth four has at most 24=16 leaves, fewer than 31 distinguishable answers. Thus no algorithm can guarantee four probes. The optimal worst case is 5, not a linear scan of 31.