-
Notifications
You must be signed in to change notification settings - Fork 29
/
0268-Missing-Number.py
51 lines (39 loc) · 1.16 KB
/
0268-Missing-Number.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
39
40
41
42
43
44
45
46
47
48
49
50
51
'''
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.
Example 1:
Input: [3,0,1]
Output: 2
Example 2:
Input: [9,6,4,2,3,5,7,0,1]
Output: 8
Note:
Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?
'''
from typing import List
# Using Math Formula to find sum
class Solution:
def missingNumber(self, nums: List[int]) -> int:
n = len(nums)
total = n*(n+1)//2
for n in nums:
total -= n
return total
# Using Bit Manipulation
class Solution:
def missingNumber(self, nums: List[int]) -> int:
res = 0
for i in range(len(nums) + 1):
res ^= i
if i < len(nums):
res ^= nums[i]
return res
# Using Math Formula and in-built sum function
class Solution:
def missingNumber(self, nums: List[int]) -> int:
current_sum = sum(nums)
intended_sum = len(nums) * (len(nums) + 1) // 2
return intended_sum - current_sum
# Check Custom Input
s = Solution()
answer = s.missingNumber([0, 3, 1, 2, 5]) # 4
print(answer)