Jan 3, 2009

In a sorted array of 0's and 1's , find the first occurrence of a 1 in it.... eg: 000111111111 must return 4 Binary search F(int a, int l, int u) { If

In a sorted array of 0's and 1's , find the first occurrence of a 1 in it....
eg:
000111111111
must return 4
Binary search

foo(int a[], int min, int max)
{
if(min==max) return min;
int mid=(min+max)/2;
if(a[mid]==1)
return foo(a,min,mid);
else
return foo(a,mid+1,max);
}

No comments:

Post a Comment