forked from SamirPaulb/DSAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create 13. Missing Number In Arithmetic Progression.py
- Loading branch information
Showing
1 changed file
with
41 additions
and
0 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
11_Binary-Search/13. Missing Number In Arithmetic Progression.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# https://www.lintcode.com/problem/3633 | ||
|
||
from typing import List | ||
|
||
########################### Solution 1 -> Binary Search ########################### | ||
class Solution: | ||
def missing_number(self, arr: List[int]) -> int: | ||
# An = A0 + (n-1) * d | ||
n = len(arr)+1 | ||
d = (arr[-1] - arr[0]) // (n-1) | ||
l, r = 0, n-2 | ||
while l <= r: | ||
m = l + (r-l)//2 | ||
nl = m+1 | ||
nr = n-m-1 | ||
if abs(arr[m] - arr[0]) // abs(d) + 1 > nl : | ||
if m != 0 and arr[m-1] != arr[m] - d: return arr[m] - d | ||
r = m - 1 | ||
else: | ||
if m != n-1 and arr[m+1] != arr[m] + d: return arr[m] + d | ||
l = m + 1 | ||
|
||
return -1 | ||
|
||
# Time: O(log(n)) | ||
# Space: O(1) | ||
|
||
|
||
########################### Solution 2 -> Math ########################### | ||
class Solution: | ||
def missing_number(self, arr: List[int]) -> int: | ||
# arr[-1] = arr[0] + (n-1)d | ||
# Sum of AP = (2*arr[0] + d(n-1)) * n//2 | ||
# Sum of AP = (arr[0] + arr[-1]) * n//2 | ||
# Removed number = sum of AP - sum of arr | ||
n = len(arr)+1 | ||
return (arr[0] + arr[-1]) * n//2 - sum(arr) | ||
|
||
# Time: O(n) as time complexity of sum() function is O(n) in python | ||
# Space: O(1) | ||
|