Problem 366
Single Element in a Sorted Array
Medium · 20 pts · Array, Binary Search
Problem
A sorted array contains integers where every value appears exactly twice, except for one value that appears only once. Find that single value. A linear scan or XOR works in O(N), but the intended solution is O(log N): before the single element, each pair starts at an even index; after it, each pair starts at an odd index. Binary search on that pattern.
Input
First line: N (odd). Second line: N sorted integers.
Output
Print the value that appears once.
Constraints
1 <= N <= 100000; N is odd; 0 <= A[i] <= 100000
Example input
9 1 1 2 3 3 4 4 8 8
Example output
2
Explanation
In 1 1 2 3 3 4 4 8 8 every value has a partner except 2, so the answer is 2.