I am studying for an exam and found this question with the following solution that I didn't understand it at all. Any help on what they did and why?
I have an unsorted array where each integer in the array appears in pairs (next to each other); in this array, there is only one integer that appears without a pair; also, there can't be two pairs of the same integer next to each other!
I need to find this odd integer in the best time (best time complexity) for example, given the following array:
8 8 5 5 3 6 6 -1 -1 7 7
the output is 3!
The code:
int FindOddOccuring(int arr[], int n) {
int left = 0, right = n / 2;
while (left < right)
{
int mid = (left + right) / 2;
if (arr[2 * mid + 1] == arr[2 * mid])
left = mid + 1;
else
right = mid;
}
return arr[2 * right];
}