1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

given array with n numbers such that arr[0] is even and arr[n-1] is odd, find index i such...

Discussion in 'Programming/Internet' started by DonaldT, Sep 12, 2018.

  1. DonaldT

    DonaldT Guest

    Suppose I have an array A with n numbers, so that the first element of this array is even, and last one is odd- I would like to write an function which find index i , such that A is even and A[i+1] is odd. The function is to return the found index; if there is not any such index, return -1.

    I have been thinking of using binary search in that case.

    Here is what I have done so far:

    int findIndex(int A[], int n)
    {
    int left=0,right=n-1,mid,i;

    while(left<=right)
    {
    mid=(left+right)\2;
    if (A[mid]%2==0 && A[mid+1]%2!=0)
    return mid;
    else if (A[mid]%2!=0)
    right=mid-1;
    else
    left=mid+1;
    }
    return -1;
    }


    I am not sure...

    What do you think about it? Any help would be very appreciated.

    Login To add answer/comment
     

Share This Page