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 < right)
{
long mid = left + ((right-left)/2);
if(mid == size -1)
return mid;
if (arr[mid] > arr[mid-1] && arr[mid] > arr[mid+1])
{
return mid;
}
if (arr[mid] > arr[mid-1])
{
left = mid;
}
else
{
right = mid-1;
}
if (arr[mid] > arr[mid+1])
{
right = mid;
}
else
{
left = mid+1;
}
}
return left;
}
arr == nil
is not C. Maybe you should change that toarr == NULL
. \$\endgroup\$ – JS1 Oct 3 '16 at 18:02