Here Optimal algorithm would be Binary search with little modification. The question implies it is a sorted array of 0's followed by 1's. So 0111...1 or 00...01 or any combination of {$0^{+}.1^{+}$} of size 31.

We are taking 2 index variable i & j and it will run until i<j.

Binary Search (Modified):-

int i, j, k;

i= 0; j = 30;

do {

k = (i+ j) / 2;

if( A[k] == 0)

i = k+1;

else j = k-1;

} while (i < j) ;

If(A[j] == 0) printf(" J+1 is the smallest index for value 1 ");

else printf(" J is the smallest index for 1 ") ;

In the below example we are taking an array of 7 element to understand the function of this algorithm, rest is same.

Here 2 probes are necessary to reach first 1's or last 0's in this sequence, and again 1 probe to check what value is in A[j] to find the correct index for first 1's.

So in general total no. of probes needed for this sequence of length 31= $\left \lceil Log 31 \right \rceil$ = 5