1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
| class Solution
{
int findOnce(int arr[], int n)
{
// Initialize pointers for binary search
int l = 0;
int r = n - 1;
while (l <= r) {
// middle index
int m = (l + r) / 2;
int current = arr[m];
// Check if the current element is equal to the previous element
if (m - 1 >= 0 && current == arr[m - 1]) {
// If `m` is odd, all the previous elements must have come up twice.
// For example: {1, 1, 2, 2, 3, 3, 4, 50, 50, 65, 65}
// Let's say m = 5
// If there was any number before this position which came once
// Then, the position of current would have shifted by 1
// Since, that is not the case, we should look towards right
if (m % 2 == 1) {
l = m + 1;
// Otherwise look left
} else {
r = m - 1;
}
}
// In the same way:
// {1, 1, 2, 2, 3, 3, 4, 50, 50, 65, 65}
// Let's say m = 4
// If there was any number before current which was unique,
// It would have shifted by one position towards left
else if (m + 1 < n && current == arr[m + 1]) {
if (m % 2 == 1) {
r = m - 1;
} else {
l = m + 1;
}
}
// If the current element is not equal to its neighbors, it is the single element
else {
return current;
}
}
// no unique
return -1;
}
}
|