-
Notifications
You must be signed in to change notification settings - Fork 0
/
single_element_in_a_sorted_array.php
56 lines (48 loc) · 1.51 KB
/
single_element_in_a_sorted_array.php
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
54
55
56
<?php
/**
* Single Element In a Sorted Array
* @link https://leetcode.com/problems/single-element-in-a-sorted-array
*
* Approach:
* Forget about values of array, focus on indices.
* Length of array partitions is what we need.
*
* 1. Determine which one is this element of number (first/left, second/right).
* 2. Based on the result of first step, check the length of left/right partition.
* If the length of partition is even, our exception is not there.
* Because each element appears exactly twice and the length of array with this property always will be even.
*
* Time Complexity - O(log n)
* Space Complexity - O(1)
*/
class Solution {
public function singleNonDuplicate($nums)
{
$l = 0;
$r = count($nums) - 1;
while (true) {
$mid = floor(($l + $r) / 2);
// second/right element
if ($nums[$mid] !== $nums[$mid + 1]) {
if ($nums[$mid] !== $nums[$mid - 1]) {
return $nums[$mid];
}
if (($r - $mid) % 2 !== 0) {
$l = $mid + 1;
} else {
$r = $mid - 2;
}
} else {
// first/left element
if ($nums[$mid] !== $nums[$mid + 1]) {
return $nums[$mid];
}
if (($mid - $l) % 2 !== 0) {
$r = $mid - 1;
} else {
$l = $mid + 2;
}
}
}
}
}