VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-missing-number-arithmetic-progression/

⇱ Missing in Arithmetic Progression - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Missing in Arithmetic Progression

Last Updated : 18 Jan, 2026

Given an array arr[] that represents elements of arithmetic progression in order. One element is missing in the progression, find the missing number. 

Note: An element will always exist that, upon inserting into a sequence forms Arithmetic progression. If the given sequence already forms a valid complete AP, return the (n+1)th element that would come next in the sequence.

Examples:

Input: arr[] = [2, 4, 8, 10, 12, 14]
Output: 6
Explanation: In this case, the common difference is 2, and 6 is missing between 4 and 8.

Input: arr[] = [1, 6, 11, 16, 21, 31]
Output: 26
Explanation: In this case, the common difference is 5, and 26 is missing between 21 and 31.

[Naive Approach] Using Mathematical Formulae – O(n) Time and O(1) Space

A simple Solution is to linearly traverse the array and find the missing number. We know that in an AP,

Sum of the n elements (s) = ((n+1)/2) * (a + (a +diff*n))
n is the number of elements, a is the first element and diff is difference between the each term of the AP. (a + diff*n) is the (n+1)th element of the AP.

If we take the sum of all elements and keep it in variable sum. We will get the missing number by s-sum (As sum doesn't includes the missing number).

To find the common difference(diff) in a nearly complete arithmetic progression. We first checks if the difference between the first two elements matches the last two elements, if so, it uses that. If not, it checks whether the first difference equals the expected average step. If both fail, it falls back to the last difference


Output
6

[Expected Approach] Using Binary Search (Recursive) – O(log n) Time and O(1) Space

The idea is to go to the middle element. Check if the difference between middle and next to middle is equal to diff or not, if not then the missing element lies between mid and mid+1. If the middle element is equal to (n/2)th term in Arithmetic Series, then missing element lies in right half. Else element lies in left half.


Output
6

[Alternate Approach] Using Binary Search (Iterative) – O(log n) Time and O(1) Space

The idea is to go to the middle element and check whether the index of middleelement is equal to (nth position of middle element in AP) minus 1. If so then the missing element lies at right half and if not then the missing element lies at left half (this idea is similar to Find the only repeating element in a sorted array of size n).

 After breaking out of binary search loop the missing element will lie between indices high and low. We can find the missing element by adding the common difference with element at index high or by subtracting the common difference with element at index low.


Output
6
Comment