Hi,
Here is the code to do this using Bsearch in o(logn) time.
int BsearchElemEqualIndex (int *a, int start, int end)
{
int mid = (((end - start) >> 1) + start);
if (a[mid] == mid)
return a[mid];
else if (a[mid] != mid)
{
if (mid == start || mid == end)
{
return -1;
}
else
{
BsearchElemEqualIndex (a, start, mid);
BsearchElemEqualIndex (a, mid + 1, end);
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
int a[SIZE] = {5,9,3,8,1,2,6,7};
int x = BsearchElemEqualIndex (a, 0, SIZE);
printf ("%d", x);
system ("PAUSE");
return 0;
}
Cheers,
Ankit!!!
On Thu, Mar 3, 2011 at 11:04 AM, Param10k <[email protected]> wrote:
> There is a sorted array and you have to write a fn to find a number in
> the array which satisfies
>
> A[i] = i ; where A is the array and i is the index...
>
> - Param
> http://teknotron-param.blogspot.com/
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>
>
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.