GATE / CS ARCHIVE

GATE CSE 2017 SET 1 / QUESTION 48

GATE CSE 2017 Set 1 Q48

Algorithms · Searching, Sorting & Selection · Full GATE CSE 2017 Set 1 paper

GATE 2017 Set 1, Q48 2 marks · NAT
Report issue

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?
Tap to enlarge
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.

Original paper, page 21 · Answer key