Find max element in increasing-decreasing array Question:  Find the largest in a list that list is stored as two sections, one in ascending order and the other in descending order.  Elements are distinct. 

E.g:  Input [2 3 4 5 6 7 10 9 8 7] 

Output: 10

My solution is below.  Works on my unit-tests.  Want to know if there any improvements from the coding point of view (esp if it is asked in an interview).

long getMaxIndex(long *arr, size_t size)
{
    long left = 0;
    long right = size - 1;

    if (arr == nil) return 0;

    while (left &lt; right)
    {
        long mid = left + ((right-left)/2);

        if(mid == size -1)
            return mid;

        if (arr[mid] &gt; arr[mid-1] &amp;&amp; arr[mid] &gt; arr[mid+1])
        {
            return mid;
        }

        if (arr[mid] &gt; arr[mid-1])
        {
            left = mid;
        }
        else
        {
            right = mid-1;
        }

        if (arr[mid] &gt; arr[mid+1])
        {
            right = mid;
        }
        else
        {
            left = mid+1;
        }
    }

    return left;
}
Your code will segfault for array size = 2. Rest looks correct to me.

Specifically, for size = 2,

left = 0
right = 1
mid = 0


And hence, this line:

if (arr[mid] &gt; arr[mid-1] &amp;&amp; arr[mid] &gt; arr[mid+1])


will try to access arr[mid-1] which is arr[-1] and can segfault/return incorrect answer.