Find Missing Number
Learn how to find the missing number in a sequence from 1 to N in Python.
Find Missing Number
Problem
Given a list containing n-1 numbers from 1 to n with exactly one number missing, find the missing number.
Input: [1, 2, 4, 5, 6] (n=6)
Output: 3
Input: [2, 3, 4, 5, 6] (n=6)
Output: 1
Input: [1, 2, 3, 4, 5] (n=6)
Output: 6
Input: [1] (n=2)
Output: 2Logic
The key insight:
If no number was missing, the sum of 1 to n is always:
sum = n × (n + 1) / 2
Example: sum of 1 to 6 = 6 × 7 / 2 = 21So if the actual sum of the list is 18, the missing number is 21 - 18 = 3.
Flow
Solution 1 — sum formula
The fastest and cleanest approach. Calculate the expected sum and subtract the actual sum.
def find_missing(numbers):
# n is the length + 1 because one number is missing
n = len(numbers) + 1
# expected sum of 1 to n using Gauss formula
expected_sum = n * (n + 1) // 2
# actual sum of the given list
actual_sum = sum(numbers)
# the difference is the missing number
return expected_sum - actual_sum
print(find_missing([1, 2, 4, 5, 6])) # 3
print(find_missing([2, 3, 4, 5, 6])) # 1
print(find_missing([1, 2, 3, 4, 5])) # 6
print(find_missing([1])) # 2Code Execution — Solution 1
Trace through find_missing([1, 2, 4, 5, 6]):
| Step | Code | Result |
|---|---|---|
n | len([1,2,4,5,6]) + 1 | 6 |
| Expected sum | 6 × 7 // 2 | 21 |
| Actual sum | 1+2+4+5+6 | 18 |
| Missing | 21 - 18 | 3 |
Trace through find_missing([2, 3, 4, 5, 6]):
| Step | Code | Result |
|---|---|---|
n | len([2,3,4,5,6]) + 1 | 6 |
| Expected sum | 6 × 7 // 2 | 21 |
| Actual sum | 2+3+4+5+6 | 20 |
| Missing | 21 - 20 | 1 |
Trace through find_missing([1, 2, 3, 4, 5]):
| Step | Code | Result |
|---|---|---|
n | len([1,2,3,4,5]) + 1 | 6 |
| Expected sum | 6 × 7 // 2 | 21 |
| Actual sum | 1+2+3+4+5 | 15 |
| Missing | 21 - 15 | 6 |
n × (n + 1) / 2 is called the Gauss formula — named after mathematician Carl Friedrich Gauss. The story goes that as a child, Gauss quickly summed 1 to 100 by pairing numbers: (1+100) + (2+99) + ... = 50 pairs × 101 = 5050. The formula captures this pattern for any n.
Solution 2 — using a loop
Manually calculate both sums by looping. More explicit — shows the steps clearly.
def find_missing(numbers):
n = len(numbers) + 1
# calculate expected sum manually
expected_sum = 0
for i in range(1, n + 1):
expected_sum += i
# calculate actual sum manually
actual_sum = 0
for num in numbers:
actual_sum += num
return expected_sum - actual_sum
print(find_missing([1, 2, 4, 5, 6])) # 3
print(find_missing([2, 3, 4, 5, 6])) # 1
print(find_missing([1, 2, 3, 4, 5])) # 6Code Execution — Solution 2
Trace through find_missing([1, 2, 4, 5, 6]):
Expected sum loop — range(1, 7) = 1, 2, 3, 4, 5, 6:
i | expected_sum |
|---|---|
1 | 1 |
2 | 3 |
3 | 6 |
4 | 10 |
5 | 15 |
6 | 21 |
Actual sum loop — [1, 2, 4, 5, 6]:
num | actual_sum |
|---|---|
1 | 1 |
2 | 3 |
4 | 7 |
5 | 12 |
6 | 18 |
Missing = 21 - 18 = 3
Solution 3 — using XOR
XOR is a bitwise operation with a special property — a XOR a = 0 and a XOR 0 = a. If you XOR a number with itself, they cancel out. XOR all numbers from 1 to n with all numbers in the list — only the missing number will not cancel.
def find_missing(numbers):
n = len(numbers) + 1
# XOR all numbers from 1 to n
xor_all = 0
for i in range(1, n + 1):
xor_all ^= i # ^= is XOR assignment
# XOR all numbers in the list
xor_list = 0
for num in numbers:
xor_list ^= num
# missing number — everything else cancelled out
return xor_all ^ xor_list
print(find_missing([1, 2, 4, 5, 6])) # 3
print(find_missing([2, 3, 4, 5, 6])) # 1
print(find_missing([1, 2, 3, 4, 5])) # 6Code Execution — Solution 3
Trace through find_missing([1, 2, 4, 5, 6]):
XOR all numbers 1 to 6:
xor_all = 1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 6
XOR all numbers in list:
xor_list = 1 ^ 2 ^ 4 ^ 5 ^ 6
Final XOR:
xor_all ^ xor_list = (1^1) ^ (2^2) ^ 3 ^ (4^4) ^ (5^5) ^ (6^6)
= 0 ^ 0 ^ 3 ^ 0 ^ 0 ^ 0
= 3
Every number that appears in both cancels to 0. Only 3 — the missing number — remains.
XOR properties:
a ^ a = 0— same number cancels itselfa ^ 0 = a— XOR with 0 changes nothing- XOR is commutative and associative — order does not matter
These properties make XOR perfect for finding a missing or duplicate element.
Solution 4 — using a set
Put all numbers in a set, then check which number from 1 to n is not in the set.
def find_missing(numbers):
n = len(numbers) + 1
# convert list to set for O(1) lookup
number_set = set(numbers)
# check each number from 1 to n
for i in range(1, n + 1):
if i not in number_set:
return i
return None # no missing number found
print(find_missing([1, 2, 4, 5, 6])) # 3
print(find_missing([2, 3, 4, 5, 6])) # 1
print(find_missing([1, 2, 3, 4, 5])) # 6Code Execution — Solution 4
Trace through find_missing([1, 2, 4, 5, 6]):
number_set = {1, 2, 4, 5, 6}
i | i in number_set? |
|---|---|
1 | Yes |
2 | Yes |
3 | No → return 3 |
Stops as soon as it finds the missing number.
Solution 5 — sorting
Sort the list then check where the sequence breaks.
def find_missing(numbers):
numbers = sorted(numbers) # sort in place copy
# check each consecutive pair
for i in range(len(numbers) - 1):
# if next number is not current + 1 — gap found
if numbers[i + 1] != numbers[i] + 1:
return numbers[i] + 1
# if no gap found inside — missing number is at one of the ends
# check if 1 is missing
if numbers[0] != 1:
return 1
# missing number must be at the end
return numbers[-1] + 1
print(find_missing([1, 2, 4, 5, 6])) # 3
print(find_missing([2, 3, 4, 5, 6])) # 1
print(find_missing([1, 2, 3, 4, 5])) # 6Code Execution — Solution 5
Trace through find_missing([4, 1, 5, 2, 6]):
After sorting: [1, 2, 4, 5, 6]
i | numbers[i] | numbers[i+1] | Gap? |
|---|---|---|---|
0 | 1 | 2 | No |
1 | 2 | 4 | Yes → return 2 + 1 = 3 |
Trace through find_missing([2, 3, 4, 5, 6]):
After sorting: [2, 3, 4, 5, 6]
No gap inside the sequence → check numbers[0] = 2 ≠ 1 → return 1
Performance comparison
| Solution | Time complexity | Space complexity | Notes |
|---|---|---|---|
| Solution 1 — sum formula | O(n) | O(1) | Best overall |
| Solution 2 — manual loop | O(n) | O(1) | Same as 1, more verbose |
| Solution 3 — XOR | O(n) | O(1) | No overflow risk |
| Solution 4 — set | O(n) | O(n) | Extra memory for the set |
| Solution 5 — sorting | O(n log n) | O(1) | Slowest — sorting costs |
Solution 1 (sum formula) has one potential issue — for very large n, the expected sum could overflow in languages like C or Java. Python handles arbitrarily large integers natively so this is not a problem here. But in interviews, Solution 3 (XOR) is often preferred since it has zero overflow risk in any language.
Which solution to use?
| Solution | How | Best when |
|---|---|---|
| Solution 1 | Sum formula | Python — cleanest and fastest |
| Solution 2 | Manual sum loop | Learning — shows the math clearly |
| Solution 3 | XOR | Interviews — no overflow, very clever |
| Solution 4 | Set lookup | When you want explicit membership check |
| Solution 5 | Sort then scan | When you need to visualize the gap |
Output
3
1
6
2