forked from SamirPaulb/DSAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSingle Element in a Sorted Array.py
38 lines (31 loc) Β· 1.24 KB
/
Single Element in a Sorted Array.py
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
# https://leetcode.com/problems/single-element-in-a-sorted-array/
'''
If every element in the sorted array were to appear exactly twice, they would occur in pairs at indices i, i+1 for all even i.
Equivalently, nums[i] = nums[i+1] and nums[i+1] != nums[i+2] for all even i.
When we insert the unique element into this list, the indices of all the pairs following it will be shifted by one, negating the above relationship.
'''
class Solution:
def singleNonDuplicate(self, nums: List[int]) -> int:
if len(nums) == 1: return nums[0]
r = len(nums)
l = 0
while l <= r:
m = (l + r) // 2
if m == 0 or m == len(nums)-1: return nums[m]
if m % 2 != 0:
if nums[m-1] != nums[m] != nums[m+1]:
return nums[m]
elif nums[m-1] != nums[m]:
r = m - 1
else:
l = m + 1
else:
if nums[m-1] != nums[m] != nums[m+1]:
return nums[m]
elif nums[m] != nums[m+1]:
r = m - 1
else:
l = m + 1
return nums[m]
# Time: O(log(n))
# Space: O(n)