⚡Flashcard Drill➔
NeetCode #266LC-1228EasyBinary SearchNC Algo100
← Back to All Problems

#266 · #1228 · Missing Number In Arithmetic Progression(等差子数组中的缺失数字)

📌 Problem Statement & Constraints

Given an array arr that was originally an arithmetic progression with a missing element, return the missing value. The progression has at least 3 elements and the common difference is non-zero. Constraints: 3 <= arr.length <= 1000, 0 <= arr[i] <= 10^5.

💡 Core Algorithmic Approaches

  1. The common difference is (arr[-1] - arr[0]) / n, where n is the length of the array (the original progression had n + 1 terms).
  2. Then compare each element with its expected value arr[0] + i * d and return the first mismatch's expected value.
  3. Because only one element is missing, at most one mismatch can occur before the gap.
  4. A binary search version compares arr[mid] with the expected value to decide which side contains the gap -- O(log n).

💻 Benchmark Python3 Implementation

class Solution:
    def missingNumber(self, arr: List[int]) -> int:
        n = len(arr)
        d = (arr[-1] - arr[0]) // n    # the original progression had n+1 terms
        for i in range(1, n):
            expected = arr[0] + i * d
            if arr[i] != expected:
                return expected
        return arr[0]                  # unreachable per the problem guarantee


# Binary-search variant: O(log n)
class Solution2:
    def missingNumber(self, arr: List[int]) -> int:
        n = len(arr)
        d = (arr[-1] - arr[0]) // n
        lo, hi = 0, n - 1
        while lo < hi:
            mid = (lo + hi) // 2
            if arr[mid] == arr[0] + mid * d:
                lo = mid + 1           # gap is to the right
            else:
                hi = mid
        return arr[0] + lo * d

⚡ Complexity Deep Dive

⏱️ Time Complexity
O(n) for the linear scan, O(log n) for the binary-search variant.
💾 Space Complexity
O(1): a few scalars.

⚠️ Interview Pitfalls & Follow-ups

  • Dividing by n - 1 instead of n: with one element missing, the array has n elements but the progression has n + 1 terms, so the divisor is n.
  • Using floating-point division: d is always an integer here (the problem guarantees a valid progression), so integer division is exact.
  • Returning arr[i] instead of the expected value: the missing element is the expected value, not the array's element.
  • Assuming the gap is at the end: it can be anywhere, so the scan must check every position.
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
🎯 Drill This Card Now➔

🧩 Related Binary Search Problems

中文速览与面试要点

等差子数组中的缺失数字

给你一个整数数组 arr,其中除了某一个元素之外,其余元素都遵循等差数列的规律。请找出这个缺失的元素。约束:3 ≤ arr.length ≤ 1000;0 ≤ arr[i] ≤ 10^5;arr 按等差数列排列,但恰好缺失一个元素;缺失的元素不是数组的第一个或最后一个元素。

核心要点

  • 先求公差(关键) :由于缺失元素不在两端, arr[-1] − arr[0] 恰好跨越 n 个间隔(n 为原等差数列的长度,等于 len(arr) ),因此公差 d = (arr[-1] − arr[0]) // len(arr) 。
  • 二分查找(标准解) :在下标 mid 处,若 arr[mid] == arr[0] + mid × d ,说明缺失元素在 mid 右侧( l = mid + 1 );否则在左侧或就是 mid( r = mid )。
  • 结束状态 : l == r 即为缺失元素的位置,答案为 arr[0] + l × d 。
  • 为什么二分成立 :缺失元素之前的所有元素都「在正确位置」,之后的所有元素都「向前偏移了一位」,因此该性质关于下标具有单调性。

常见陷阱

  • 公差用 arr[1] − arr[0] 计算 :若缺失元素恰好是第二个元素,这个差值会是 2d 而非 d 。必须用 (arr[-1] − arr[0]) // len(arr) 。
  • 公差公式除以 len(arr) − 1 :应为 len(arr) 。因为缺失元素不在两端, arr[-1] − arr[0] 跨越了 len(arr) 个间隔。
  • 用线性扫描 :O(n) 在 n = 1000 时虽可过,但未利用「等差」的结构;二分是 O(log n)。
  • 返回值写成 arr[l] :缺失元素不在数组中,必须通过公式 arr[0] + l × d 计算。

参考实现

class Solution:
    def missingNumber(self, arr: list[int]) -> int:
        n = len(arr)
        d = (arr[-1] - arr[0]) // n      # 缺失元素不在两端,故恰好 n 个间隔
        l, r = 0, n - 1
        while l < r:
            mid = (l + r) // 2
            if arr[mid] == arr[0] + mid * d:
                l = mid + 1              # 该位置正确 -> 缺失在右侧
            else:
                r = mid                  # 该位置已偏移 -> 缺失在左侧或就是 mid
        return arr[0] + l * d