-
-
Notifications
You must be signed in to change notification settings - Fork 50.5k
Expand file tree
/
Copy pathmissing_number.py
More file actions
50 lines (45 loc) · 1.58 KB
/
missing_number.py
File metadata and controls
50 lines (45 loc) · 1.58 KB
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
def find_missing_number(nums: list[int]) -> int:
"""
Finds the missing number in a list of consecutive integers.
Uses XOR to find the missing number efficiently. XOR has the property that:
- a ^ a = 0 (any number XORed with itself is 0)
- a ^ 0 = a (any number XORed with 0 is itself)
- XOR is commutative and associative
Therefore, XORing all numbers with all indices will cancel out the existing
numbers, leaving only the missing number.
Algorithm:
1. Initialize result with the maximum value from the list
2. For each position i and corresponding value nums[i]:
a. XOR result with the position index i
b. XOR result with the value nums[i]
3. The result will be the missing number (all others cancel out)
Why it works:
If list is [0,1,3,4], we need to find 2
- Low = 0, High = 4
- XOR all values and indices: result = 4 ^ 0 ^ 1 ^ 3 ^ 4 ^ 0 ^ 1 ^ 3
- When simplified: all numbers except 2 appear twice, so they cancel
- Result: 2
>>> find_missing_number([0, 1, 3, 4])
2
>>> find_missing_number([4, 3, 1, 0])
2
>>> find_missing_number([-4, -3, -1, 0])
-2
>>> find_missing_number([-2, 2, 1, 3, 0])
-1
>>> find_missing_number([1, 3, 4, 5, 6])
2
>>> find_missing_number([6, 5, 4, 2, 1])
3
>>> find_missing_number([6, 1, 5, 3, 4])
2
"""
low = min(nums)
high = max(nums)
missing_number = high
for i in range(low, high):
missing_number ^= i ^ nums[i - low]
return missing_number
if __name__ == "__main__":
import doctest
doctest.testmod()