Missing Number in AP

Pattern: Binary search on sorted arrays

medium

In an arithmetic progression with one term missing, every value before the gap equals first + i × d, and every value after it does not. Binary search for the first index where the formula fails.

No verified practice link yet. The guide below states the problem in full, so you can still solve it here.

The problem

You are given an arithmetic progression (each term is the previous one plus the same step d) with exactly one term missing. The missing term is never the first or the last. Return it. For example, [2, 4, 8, 10, 12, 14] gives 6, and [1, 6, 11, 16, 21, 31] gives 26. The step may be negative, as in [20, 15, 5, 0], which gives 10.

This problem is from Love Babbar's 450 sheet, and this guide has no verified link for it yet. The function name findMissing is this guide's own.

Think first

  • With the first and last terms known and one term missing, how many steps lie between them?
  • Before the missing term, what is arr[i] exactly? After it?
  • Which yes/no test splits those two parts?

Hints

    Brute force, and why it fails

    Walk the array and return the first place where the step between neighbours is not d: O(n). Or compare the expected sum with the real sum. Binary search is O(log n).

    The key insight

    The missing term shifts every later term by one position. That makes "value matches its expected position" a no/yes test with one boundary, and the boundary is the answer.

    Dry run

    20lo
    41 
    82 
    103 
    124 
    145hi
    d2

    arr = [2, 4, 8, 10, 12, 14] has 6 values. With the missing term back there would be 6 gaps, so d = (14 - 2) / 6 = 2.

    Step 1 of 4

    The plan

    d = (arr[n - 1] - arr[0]) / n
    lo = 0, hi = n - 1
    while lo < hi:
        mid = lo + (hi - lo) / 2
        if arr[mid] == arr[0] + mid * d: lo = mid + 1
        else: hi = mid
    return arr[0] + lo * d
    

    Complexity

    Time
    O(log n)
    Space
    O(1)

    Finding d is O(1), and the search halves the range each step.

    Edge cases

    • Two values, [1, 3]: d = 1 and the answer is 2.
    • A falling progression: d is negative and the same formula works.
    • d = 0 would mean nothing is missing, which the statement rules out.
    I've built my logic, show the code

    Java

    class Solution {
        public int findMissing(int[] arr) {
            int n = arr.length;
            int d = (arr[n - 1] - arr[0]) / n;      // n gaps would be there with the missing term put back
            int lo = 0, hi = n - 1;                 // find the first index whose value is not the expected one
            while (lo < hi) {
                int mid = lo + (hi - lo) / 2;
                if (arr[mid] == arr[0] + mid * d) lo = mid + 1;   // everything up to mid is in place
                else hi = mid;                                    // the gap is at mid or before it
            }
            return arr[0] + lo * d;
        }
    }

    C++

    class Solution {
    public:
        int findMissing(vector<int>& arr) {
            int n = static_cast<int>(arr.size());
            int d = (arr[n - 1] - arr[0]) / n;      // n gaps would be there with the missing term put back
            int lo = 0, hi = n - 1;                 // find the first index whose value is not the expected one
            while (lo < hi) {
                int mid = lo + (hi - lo) / 2;
                if (arr[mid] == arr[0] + mid * d) lo = mid + 1;   // everything up to mid is in place
                else hi = mid;                                    // the gap is at mid or before it
            }
            return arr[0] + lo * d;
        }
    };

    Python

    class Solution:
        def findMissing(self, arr: list[int]) -> int:
            n = len(arr)
            d = (arr[-1] - arr[0]) // n             # n gaps would be there with the missing term put back
            lo, hi = 0, n - 1                       # find the first index whose value is not the expected one
            while lo < hi:
                mid = (lo + hi) // 2
                if arr[mid] == arr[0] + mid * d:
                    lo = mid + 1                    # everything up to mid is in place
                else:
                    hi = mid                        # the gap is at mid or before it
            return arr[0] + lo * d

    Common mistakes

    • Taking d = arr[1] - arr[0], which is wrong when the gap is right after the first term.
    • Dividing by n - 1 instead of n. With the missing term put back there are n steps.
    • Comparing neighbours inside the binary search, which does not tell you which side the gap is on.

    Similar problems