Prerequisites

Before attempting this problem, you should be comfortable with:

  • Arithmetic Progression - Understanding how to calculate the common difference and expected values at each position in a sequence
  • Binary Search - The optimal solution uses binary search to locate the missing element by comparing actual vs expected values

Intuition

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.

Algorithm

  1. Calculate the common difference: difference = (arr[n - 1] - arr[0]) / n.
  2. Initialize expected = arr[0].
  3. Iterate through each element in the array:
    • If the current element does not equal expected, return expected as the missing number.
    • Otherwise, increment expected by difference.
  4. If no mismatch is found during iteration, return the final 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 expected

Time & Space Complexity

  • Time complexity: O(n)O(n)
  • Space complexity: O(1)O(1) constant space

Where nn is the length of array arr.


Intuition

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.

Algorithm

  1. Calculate the common difference: difference = (arr[n - 1] - arr[0]) / n.
  2. Initialize lo = 0 and hi = n - 1.
  3. While lo < hi:
    • Compute mid = (lo + hi) / 2.
    • If arr[mid] equals the expected value arr[0] + mid * difference, the missing element is after mid, so set lo = mid + 1.
    • Otherwise, the missing element is at or before mid, so set hi = mid.
  4. Return 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 * lo

Time & Space Complexity

  • Time complexity: O(logn)O(\log n)
  • Space complexity: O(1)O(1) constant space

Where nn is the length of array arr.


Common Pitfalls

Incorrect Common Difference Calculation

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.

Not Handling Zero Difference

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.

Integer Division Truncation

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.