Skip to content
Official PYQ · 2017

GATE CS 2017 Set 1 Q48 — Searching, Sorting, Hashing

Explain with AI

Let AA be an array of 3131 numbers consisting of a sequence of 00's followed by a sequence of 11's. The problem is to find the smallest index ii such that A[i]A\left [i \right ] is 11 by probing the minimum number of locations in AA. 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 00 to 11 in a sorted array AA of size 3131. This can be modeled as a binary search problem.

Method 1: Decision Tree / Information Theory

Let the array indices be 0,1,…,300, 1, \dots, 30. The first occurrence of 11 can be at any of these 3131 indices, or there might be no 11 at all in the array. This gives us a total of 3232 possible outcomes.

Each probe at an index A[j]A[j] yields a binary outcome: either 00 or 11. Therefore, any decision tree representing an optimal search algorithm must have at least 3232 leaves.

The minimum height (worst-case number of probes) of a binary decision tree with LL leaves is given by:

Worst-case probes=⌈log⁡2(L)⌉=⌈log⁡2(32)⌉=5\text{Worst-case probes} = \lceil \log_2(L) \rceil = \lceil \log_2(32) \rceil = 5

Method 2: Binary Search Execution

We can find the transition index using standard binary search over the range [0,30][0, 30]:

  1. Probe 1: Query the middle element at index 1515.
    • If A[15]=1A[15] = 1, the first 11 lies in the range [0,15][0, 15] (size 1616).
    • If A[15]=0A[15] = 0, the first 11 lies in the range [16,30][16, 30] (size 1515).
    In the worst case, the remaining search space is of size 1616.
  2. Probe 2: Halves the search space to size 88.
  3. Probe 3: Halves the search space to size 44.
  4. Probe 4: Halves the search space to size 22.
  5. Probe 5: Halves the search space to size 11, 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.

Published
Report a correction
Keep exploringMore Searching, Sorting, Hashing questions