-
Notifications
You must be signed in to change notification settings - Fork 40
/
find_missing_number.py
49 lines (43 loc) · 1.48 KB
/
find_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
#!/usr/bin/python
# Date: 2020-11-29
#
# Description:
# Given an array with all the numbers from 1 to N appearing exactly once, except
# for one number that is missing. Find the missing number.
#
# Approach:
# - Take sum of all numbers in array
# - Subtract above sum from n*(n + 1) // 2
# - Above approach works but in some programming language, we might face
# overflow issue with sum taken so we can use alternative approach using XOR
#
# Alternative approach - takes XOR of all numbers in array and then further
# XORs with all numbers from 1 to N to nullify all non missing number.
#
# Complexity:
# O(N)
def find_missing_number(A):
N = len(A) + 1 # Range of numbers is from 1 to N
array_sum = sum(A)
expected_sum = N * (N + 1) // 2
return expected_sum - array_sum
def find_missing_number_using_xor(A):
"""
Alternative approach - takes XOR of all numbers in array and then further
XORs with all numbers from 1 to N to nullify all non missing number.
"""
N = len(A) + 1 # Range of numbers is from 1 to N
missing_number = 0
for n in A:
missing_number ^= n
i = 1
while i <= N:
missing_number ^= i
i += 1
return missing_number
assert find_missing_number([3, 1, 2, 4]) == 5
assert find_missing_number([1]) == 2
assert find_missing_number([1, 2, 3, 4, 5, 6]) == 7
assert find_missing_number_using_xor([3, 1, 2, 4]) == 5
assert find_missing_number_using_xor([1]) == 2
assert find_missing_number_using_xor([1, 2, 3, 4, 5, 6]) == 7