A Simple Solution is to linearly traverse the array and find the missing number. Time complexity of this solution is O(n).
We can solve this problem in O(Logn) time using Binary Search. 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/2th term in Arithmetic Series (Let n be the number of elements in input array), then missing element lies in right half. Else element lies in left half.
Following is implementation of above idea.
link brightness_4 code
// C++ program to find the missing number
// in a given arithmetic progression
#define INT_MAX 2147483647;
// A binary search based recursive function that returns
// the missing element in arithmetic progression
// There must be two elements to find the missing
if(high <= low)
// Find index of middle element
intmid = low + (high - low) / 2;
// The element just after the middle element is missing.
// The arr[mid+1] must exist, because we return when
// (low == high) and take floor of (high-low)/2
if(arr[mid + 1] - arr[mid] != diff)
return(arr[mid] + diff);
// The element just before mid is missing
if(mid > 0 && arr[mid] - arr[mid - 1] != diff)
return(arr[mid - 1] + diff);
// If the elements till mid follow AP, then recur
// for right half
if(arr[mid] == arr + mid * diff)
returnfindMissingUtil(arr, mid + 1,
// Else recur for left half
returnfindMissingUtil(arr, low, mid - 1, diff);
// The function uses findMissingUtil() to
// find the missing element in AP.
// It assumes that there is exactly one
// missing element and may give incorrect result
// when there is no missing element or
// more than one missing elements. This function
// also assumes that the difference in AP is an
// If exactly one element is missing, then we can find
// difference of arithmetic progression using following