Before attempting this problem, you should be comfortable with:
An arithmetic progression has a constant difference between consecutive elements. Since exactly one element is missing, we can compute what the common difference should be using the first and last elements: difference = (arr[n-1] - arr[0]) / n. Then we walk through the array, checking if each element matches the expected value. The first mismatch reveals the missing number.
difference = (arr[n - 1] - arr[0]) / n.expected = arr[0].expected, return expected as the missing number.expected by difference.expected value as the missing number.class Solution:
def missingNumber(self, arr: List[int]) -> int:
n = len(arr)
# Get the difference `difference`.
difference = (arr[-1] - arr[0]) // n
# The expected element equals the starting element.
expected = arr[0]
for val in arr:
# Return the expected value that doesn't match val.
if val != expected:
return expected
# Next element will be expected element + `difference`.
expected += difference
return expectedWhere is the length of array
arr.
Since the array is sorted and follows an arithmetic progression, we can use binary search to locate the missing element faster. At any index i, the expected value is arr[0] + i * difference. If the actual value matches, all elements up to that index are correct, so the missing number is to the right. If there is a mismatch, the missing number is at or before that index. This allows us to narrow down the search space logarithmically.
difference = (arr[n - 1] - arr[0]) / n.lo = 0 and hi = n - 1.lo < hi:mid = (lo + hi) / 2.arr[mid] equals the expected value arr[0] + mid * difference, the missing element is after mid, so set lo = mid + 1.mid, so set hi = mid.arr[0] + difference * lo as the missing number that should be at index lo.class Solution:
def missingNumber(self, arr: List[int]) -> int:
n = len(arr)
# Get the difference `difference`.
difference = (arr[n - 1] - arr[0]) // n
lo = 0
hi = n - 1
# Basic binary search template.
while lo < hi:
mid = (lo + hi) // 2
# All numbers up to `mid` have no missing number, so search on the right side.
if arr[mid] == arr[0] + mid * difference:
lo = mid + 1
# A number is missing before `mid` inclusive of `mid` itself.
else:
hi = mid
# Index `lo` will be the position with the first incorrect number.
# Return the value that was supposed to be at this index.
return arr[0] + difference * loWhere is the length of array
arr.
The common difference must be calculated as (arr[n-1] - arr[0]) / n, not (arr[n-1] - arr[0]) / (n-1). Since one element is missing, the full sequence would have n+1 elements with n gaps, making the divisor n.
When the common difference is zero (all elements are the same), any position could be the "missing" element since the missing value equals all existing values. The linear search returns the first element in this case, which is correct.
In languages where division truncates toward zero, negative arithmetic progressions may produce incorrect common differences. Ensure the division correctly handles both positive and negative sequences.