Tuesday, December 10, 2013

[Fab.com] Occurence of first 1 in the sorted array of 0s and 1s

Find the ocuurence of first 1 in the array which contains 0s and 1s in the sorted manner.

1 comment:

  1. http://ideone.com/ptP0p4


    #include
    #define N 6
    int BinarySearch(int input[N], int beg, int end )
    {
    int mid = (beg+end)/2;

    if (input[mid] == 0)
    return BinarySearch(input, mid+1, end);
    else
    {
    if(input[mid-1] == 0)
    return mid;
    else
    return BinarySearch(input, beg, mid-1);
    }
    }

    int main()
    {
    int a[N]={0,0,0,1,1,1};
    printf("1 starts at index %d", BinarySearch(a,0,N-1));
    }

    ReplyDelete