Skip to content

Commit 02942d7

Browse files
authored
Create missing-number-in-arithmetic-progression.py
1 parent f873136 commit 02942d7

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Time: O(logn)
2+
# Space: O(1)
3+
4+
class Solution(object):
5+
def missingNumber(self, arr):
6+
"""
7+
:type arr: List[int]
8+
:rtype: int
9+
"""
10+
def check(arr, d, x):
11+
return arr[x] != arr[0] + d*x
12+
13+
d = (arr[-1]-arr[0])//len(arr)
14+
left, right = 0, len(arr)-1
15+
while left <= right:
16+
mid = left + (right-left)//2
17+
if check(arr, d, mid):
18+
right = mid-1
19+
else:
20+
left = mid+1
21+
return arr[0] + d*left

0 commit comments

Comments
 (0)