# Find the missing number in Arithmetic Progression

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

**Examples: **

Input: arr[] = {2, 4, 8, 10, 12, 14} Output: 6 Input: arr[] = {1, 6, 11, 16, 21, 31}; Output: 26

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.

## C++

`// C++ program to find the missing number` `// in a given arithmetic progression` `#include<iostream>` `using` `namespace` `std;` `#define INT_MAX 2147483647;` `class` `GFG` `{` ` ` `// A binary search based recursive function that returns` `// the missing element in arithmetic progression` `public` `:` `int` `findMissingUtil(` `int` `arr[], ` `int` `low,` ` ` `int` `high, ` `int` `diff)` `{` ` ` `// There must be two elements to find the missing` ` ` `if` `(high <= low)` ` ` `return` `INT_MAX;` ` ` `// Find index of middle element` ` ` `int` `mid = 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[0] + mid * diff)` ` ` `return` `findMissingUtil(arr, mid + 1,` ` ` `high, diff);` ` ` `// Else recur for left half` ` ` `return` `findMissingUtil(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` `// integer.` `int` `findMissing(` `int` `arr[], ` `int` `n)` `{` ` ` `// If exactly one element is missing, then we can find` ` ` `// difference of arithmetic progression using following` ` ` `// formula. Example, 2, 4, 6, 10, diff = (10-2)/4 = 2.` ` ` `// The assumption in formula is that the difference is` ` ` `// an integer.` ` ` `int` `diff = (arr[n - 1] - arr[0]) / n;` ` ` `// Binary search for the missing` ` ` `// number using above calculated diff` ` ` `return` `findMissingUtil(arr, 0, n - 1, diff);` `}` `};` `// Driver Code` `int` `main()` `{` ` ` `GFG g;` ` ` `int` `arr[] = {2, 4, 8, 10, 12, 14};` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]);` ` ` `cout << ` `"The missing element is "` ` ` `<< g.findMissing(arr, n);` ` ` `return` `0;` `}` `// This code is contributed by Soumik` |

## C

`// A C program to find the missing number in a given` `// arithmetic progression` `#include <stdio.h>` `#include <limits.h>` `// A binary search based recursive function that returns` `// the missing element in arithmetic progression` `int` `findMissingUtil(` `int` `arr[], ` `int` `low, ` `int` `high, ` `int` `diff)` `{` ` ` `// There must be two elements to find the missing` ` ` `if` `(high <= low)` ` ` `return` `INT_MAX;` ` ` `// Find index of middle element` ` ` `int` `mid = 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[0] + mid*diff)` ` ` `return` `findMissingUtil(arr, mid+1, high, diff);` ` ` `// Else recur for left half` ` ` `return` `findMissingUtil(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` `// integer.` `int` `findMissing(` `int` `arr[], ` `int` `n)` `{` ` ` `// If exactly one element is missing, then we can find` ` ` `// difference of arithmetic progression using following` ` ` `// formula. Example, 2, 4, 6, 10, diff = (10-2)/4 = 2.` ` ` `// The assumption in formula is that the difference is` ` ` `// an integer.` ` ` `int` `diff = (arr[n-1] - arr[0])/n;` ` ` `// Binary search for the missing number using above` ` ` `// calculated diff` ` ` `return` `findMissingUtil(arr, 0, n-1, diff);` `}` `/* Driver program to check above functions */` `int` `main()` `{` ` ` `int` `arr[] = {2, 4, 8, 10, 12, 14};` ` ` `int` `n = ` `sizeof` `(arr)/` `sizeof` `(arr[0]);` ` ` `printf` `(` `"The missing element is %d"` `, findMissing(arr, n));` ` ` `return` `0;` `}` |

## Java

`// A Java program to find` `// the missing number in` `// a given arithmetic` `// progression` `import` `java.io.*;` `class` `GFG` `{` ` ` `// A binary search based` `// recursive function that` `// returns the missing` `// element in arithmetic` `// progression` `static` `int` `findMissingUtil(` `int` `arr[], ` `int` `low,` ` ` `int` `high, ` `int` `diff)` `{` ` ` `// There must be two elements` ` ` `// to find the missing` ` ` `if` `(high <= low)` ` ` `return` `Integer.MAX_VALUE;` ` ` `// Find index of` ` ` `// middle element` ` ` `int` `mid = 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[` `0` `] + mid * diff)` ` ` `return` `findMissingUtil(arr, mid + ` `1` `,` ` ` `high, diff);` ` ` `// Else recur for left half` ` ` `return` `findMissingUtil(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 integer.` `static` `int` `findMissing(` `int` `arr[], ` `int` `n)` `{` ` ` `// If exactly one element is missing,` ` ` `// then we can find difference of` ` ` `// arithmetic progression using` ` ` `// following formula. Example, 2, 4,` ` ` `// 6, 10, diff = (10-2)/4 = 2.` ` ` `// The assumption in formula is that` ` ` `// the difference is an integer.` ` ` `int` `diff = (arr[n - ` `1` `] - arr[` `0` `]) / n;` ` ` `// Binary search for the missing` ` ` `// number using above calculated diff` ` ` `return` `findMissingUtil(arr, ` `0` `, n - ` `1` `, diff);` `}` `// Driver Code` `public` `static` `void` `main (String[] args)` `{` ` ` `int` `arr[] = {` `2` `, ` `4` `, ` `8` `, ` `10` `, ` `12` `, ` `14` `};` ` ` `int` `n = arr.length;` ` ` `System.out.println(` `"The missing element is "` `+` ` ` `findMissing(arr, n));` `}` `}` `// This code is contributed by anuj_67.` |

## Python3

`# A Python3 program to find the missing` `# number in a given arithmetic progression` `import` `sys` `# A binary search based recursive function` `# that returns the missing element in` `# arithmetic progression` `def` `findMissingUtil(arr, low, high, diff):` ` ` `# There must be two elements to` ` ` `# find the missing` ` ` `if` `(high <` `=` `low):` ` ` `return` `sys.maxsize;` ` ` `# Find index of middle element` ` ` `mid ` `=` `int` `(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` `and` `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[` `0` `] ` `+` `mid ` `*` `diff):` ` ` `return` `findMissingUtil(arr, mid ` `+` `1` `,` ` ` `high, diff);` ` ` `# Else recur for left half` ` ` `return` `findMissingUtil(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 integer.` `def` `findMissing(arr, n):` ` ` `# If exactly one element is missing, then` ` ` `# we can find difference of arithmetic` ` ` `# progression using following formula.` ` ` `# Example, 2, 4, 6, 10, diff = (10-2)/4 = 2.` ` ` `# The assumption in formula is that the` ` ` `# difference is an integer.` ` ` `diff ` `=` `int` `((arr[n ` `-` `1` `] ` `-` `arr[` `0` `]) ` `/` `n);` ` ` `# Binary search for the missing number` ` ` `# using above calculated diff` ` ` `return` `findMissingUtil(arr, ` `0` `, n ` `-` `1` `, diff);` `# Driver Code` `arr ` `=` `[` `2` `, ` `4` `, ` `8` `, ` `10` `, ` `12` `, ` `14` `];` `n ` `=` `len` `(arr);` `print` `(` `"The missing element is"` `,` ` ` `findMissing(arr, n));` `# This code is contributed by chandan_jnu` |

## C#

`// A C# program to find` `// the missing number in` `// a given arithmetic` `// progression` `using` `System;` `class` `GFG` `{` ` ` `// A binary search based` `// recursive function that` `// returns the missing` `// element in arithmetic` `// progression` `static` `int` `findMissingUtil(` `int` `[]arr, ` `int` `low,` ` ` `int` `high, ` `int` `diff)` `{` ` ` `// There must be two elements` ` ` `// to find the missing` ` ` `if` `(high <= low)` ` ` `return` `int` `.MaxValue;` ` ` `// Find index of` ` ` `// middle element` ` ` `int` `mid = 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[0] +` ` ` `mid * diff)` ` ` `return` `findMissingUtil(arr, mid + 1,` ` ` `high, diff);` ` ` `// Else recur for left half` ` ` `return` `findMissingUtil(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 integer.` `static` `int` `findMissing(` `int` `[]arr, ` `int` `n)` `{` ` ` `// If exactly one element` ` ` `// is missing, then we can` ` ` `// find difference of arithmetic` ` ` `// progression using following` ` ` `// formula. Example, 2, 4, 6, 10,` ` ` `// diff = (10-2)/4 = 2.The assumption` ` ` `// in formula is that the difference` ` ` `// is an integer.` ` ` `int` `diff = (arr[n - 1] -` ` ` `arr[0]) / n;` ` ` `// Binary search for the` ` ` `// missing number using` ` ` `// above calculated diff` ` ` `return` `findMissingUtil(arr, 0,` ` ` `n - 1, diff);` `}` `// Driver Code` `public` `static` `void` `Main ()` `{` ` ` `int` `[]arr = {2, 4, 8,` ` ` `10, 12, 14};` ` ` `int` `n = arr.Length;` ` ` `Console.WriteLine(` `"The missing element is "` `+` ` ` `findMissing(arr, n));` `}` `}` `// This code is contributed by anuj_67.` |

## PHP

`<?php` `// A PHP program to find the missing` `// number in a given arithmetic progression` `// A binary search based recursive function` `// that returns the missing element in` `// arithmetic progression` `function` `findMissingUtil(` `$arr` `, ` `$low` `, ` `$high` `, ` `$diff` `)` `{` ` ` `// There must be two elements to` ` ` `// find the missing` ` ` `if` `(` `$high` `<= ` `$low` `)` ` ` `return` `PHP_INT_MAX;` ` ` `// Find index of middle element` ` ` `$mid` `= ` `$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` `[0] + ` `$mid` `* ` `$diff` `)` ` ` `return` `findMissingUtil(` `$arr` `, ` `$mid` `+ 1,` ` ` `$high` `, ` `$diff` `);` ` ` `// Else recur for left half` ` ` `return` `findMissingUtil(` `$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 integer.` `function` `findMissing(` `$arr` `, ` `$n` `)` `{` ` ` `// If exactly one element is missing, then` ` ` `// we can find difference of arithmetic` ` ` `// progression using following formula.` ` ` `// Example, 2, 4, 6, 10, diff = (10-2)/4 = 2.` ` ` `// The assumption in formula is that the` ` ` `// difference is an integer.` ` ` `$diff` `= (` `$arr` `[` `$n` `- 1] - ` `$arr` `[0]) / ` `$n` `;` ` ` `// Binary search for the missing number` ` ` `// using above calculated diff` ` ` `return` `findMissingUtil(` `$arr` `, 0, ` `$n` `- 1, ` `$diff` `);` `}` `// Driver Code` `$arr` `= ` `array` `(2, 4, 8, 10, 12, 14);` `$n` `= sizeof(` `$arr` `);` `echo` `"The missing element is "` `,` ` ` `findMissing(` `$arr` `, ` `$n` `);` `// This code is contributed by Sach_Code` `?>` |

## Javascript

`<script>` ` ` `// A JavaScript program to find` ` ` `// the missing number in` ` ` `// a given arithmetic` ` ` `// progression` ` ` ` ` `// A binary search based` ` ` `// recursive function that` ` ` `// returns the missing` ` ` `// element in arithmetic` ` ` `// progression` ` ` `function` `findMissingUtil(arr, low, high, diff)` ` ` `{` ` ` `// There must be two elements` ` ` `// to find the missing` ` ` `if` `(high <= low)` ` ` `return` `Number.MAX_VALUE;` ` ` `// Find index of` ` ` `// middle element` ` ` `let mid = low + parseInt((high - low) / 2, 10);` ` ` `// 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[0] + mid * diff)` ` ` `return` `findMissingUtil(arr, mid + 1, high, diff);` ` ` `// Else recur for left half` ` ` `return` `findMissingUtil(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 integer.` ` ` `function` `findMissing(arr, n)` ` ` `{` ` ` `// If exactly one element is missing,` ` ` `// then we can find difference of` ` ` `// arithmetic progression using` ` ` `// following formula. Example, 2, 4,` ` ` `// 6, 10, diff = (10-2)/4 = 2.` ` ` `// The assumption in formula is that` ` ` `// the difference is an integer.` ` ` `let diff = parseInt((arr[n - 1] - arr[0]) / n, 10);` ` ` `// Binary search for the missing` ` ` `// number using above calculated diff` ` ` `return` `findMissingUtil(arr, 0, n - 1, diff);` ` ` `}` ` ` ` ` `let arr = [2, 4, 8, 10, 12, 14];` ` ` `let n = arr.length;` ` ` `document.write(` `"The missing element is "` `+` ` ` `findMissing(arr, n));` `</script>` |

**Output**

The missing element is 6

**Iterative: **

The idea is to go to the middle element. Check if the index of middle element is equal to (nth position of middle element in AP) – 1 then the missing element lies at right half 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 high and low. We can find the missing element by adding a common difference with element at index high or by subtracting a common difference with element at index low.

Following is implementation of above idea.

## C++

`// C++ program to find the missing number` `// in a given arithmetic progression` `#include<iostream>` `using` `namespace` `std;` `#define INT_MAX 2147483647;` `class` `GFG` `{` ` ` `// A binary search based function that returns` `// the missing element in arithmetic progression` `public` `:` `int` `findMissingUtil(` `int` `arr[], ` `int` `low,` ` ` `int` `high, ` `int` `diff)` `{ ` ` ` `// Find index of middle element` ` ` `int` `mid;` ` ` `while` `(low <= high)` ` ` `{ ` ` ` `// find index of middle element` ` ` `mid = (low + high) / 2;` ` ` `// if mid == (nth position of element in AP)-1` ` ` `// the missing element will exist in right half` ` ` `if` `((arr[mid] - arr[0]) / diff == mid)` ` ` `low = mid + 1;` ` ` `else` ` ` `// the missing element will exist in left half` ` ` `high = mid - 1;` ` ` `}` ` ` `// after breaking out of binary search loop` ` ` `// our missing element will exist between high and low` ` ` `// our missing element will be a[high] + commom difference` ` ` `// or a[low] - commom difference` ` ` `return` `arr[high] + 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` `// integer.` `int` `findMissing(` `int` `arr[], ` `int` `n)` `{` ` ` `// If exactly one element is missing, then we can find` ` ` `// difference of arithmetic progression using following` ` ` `// formula. Example, 2, 4, 6, 10, diff = (10-2)/4 = 2.` ` ` `// The assumption in formula is that the difference is` ` ` `// an integer.` ` ` `int` `diff = (arr[n - 1] - arr[0]) / n;` ` ` `// Binary search for the missing` ` ` `// number using above calculated diff` ` ` `return` `findMissingUtil(arr, 0, n - 1, diff);` `}` `};` `// Driver Code` `int` `main()` `{` ` ` `GFG g;` ` ` `int` `arr[] = {2, 4, 8, 10, 12, 14};` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]);` ` ` `cout << ` `"The missing element is "` ` ` `<< g.findMissing(arr, n);` ` ` `return` `0;` `}` `// This code is contributed by gurudev620gs` |

## C

`// A C program to find the missing number in a given` `// arithmetic progression` `#include <stdio.h>` `#include <limits.h>` `// A binary search based function that returns` `// the missing element in arithmetic progression` `int` `findMissingUtil(` `int` `arr[], ` `int` `low, ` `int` `high, ` `int` `diff)` `{` ` ` `// Find index of middle element` ` ` `int` `mid;` ` ` `while` `(low <= high)` ` ` `{ ` ` ` `// find index of middle element` ` ` `mid = (low + high) / 2;` ` ` `// if mid == (nth position of element in AP)-1` ` ` `// the missing element will exist in right half` ` ` `if` `((arr[mid] - arr[0]) / diff == mid)` ` ` `low = mid + 1;` ` ` `else` ` ` `// the missing element will exist in left half` ` ` `high = mid - 1;` ` ` `}` ` ` `// after breaking out of binary search loop` ` ` `// our missing element will exist between high and low` ` ` `// our missing element will be a[high] + commom difference` ` ` `// or a[low] - commom difference` ` ` `return` `arr[high] + 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` `// integer.` `int` `findMissing(` `int` `arr[], ` `int` `n)` `{` ` ` `// If exactly one element is missing, then we can find` ` ` `// difference of arithmetic progression using following` ` ` `// formula. Example, 2, 4, 6, 10, diff = (10-2)/4 = 2.` ` ` `// The assumption in formula is that the difference is` ` ` `// an integer.` ` ` `int` `diff = (arr[n-1] - arr[0])/n;` ` ` `// Binary search for the missing number using above` ` ` `// calculated diff` ` ` `return` `findMissingUtil(arr, 0, n-1, diff);` `}` `/* Driver program to check above functions */` `int` `main()` `{` ` ` `int` `arr[] = {2, 4, 8, 10, 12, 14};` ` ` `int` `n = ` `sizeof` `(arr)/` `sizeof` `(arr[0]);` ` ` `printf` `(` `"The missing element is %d"` `, findMissing(arr, n));` ` ` `return` `0;` `}` |

## Java

`// A Java program to find` `// the missing number in` `// a given arithmetic` `// progression` `import` `java.io.*;` `class` `GFG` `{` ` ` `// A binary search function that` `// returns the missing` `// element in arithmetic` `// progression` `static` `int` `findMissingUtil(` `int` `arr[], ` `int` `low,` ` ` `int` `high, ` `int` `diff)` `{ ` ` ` `// Find index of middle element` ` ` `int` `mid;` ` ` `while` `(low <= high)` ` ` `{ ` ` ` `// find index of middle element` ` ` `mid = (low + high) / ` `2` `;` ` ` `// if mid == (nth position of element in AP)-1` ` ` `// the missing element will exist in right half` ` ` `if` `((arr[mid] - arr[` `0` `]) / diff == mid)` ` ` `low = mid + ` `1` `;` ` ` `else` ` ` `// the missing element will exist in left half` ` ` `high = mid - ` `1` `;` ` ` `}` ` ` `// after breaking out of binary search loop` ` ` `// our missing element will exist between high and low` ` ` `// our missing element will be a[high] + commom difference` ` ` `// or a[low] - commom difference` ` ` `return` `arr[high] + 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 integer.` `static` `int` `findMissing(` `int` `arr[], ` `int` `n)` `{` ` ` `// If exactly one element is missing,` ` ` `// then we can find difference of` ` ` `// arithmetic progression using` ` ` `// following formula. Example, 2, 4,` ` ` `// 6, 10, diff = (10-2)/4 = 2.` ` ` `// The assumption in formula is that` ` ` `// the difference is an integer.` ` ` `int` `diff = (arr[n - ` `1` `] - arr[` `0` `]) / n;` ` ` `// Binary search for the missing` ` ` `// number using above calculated diff` ` ` `return` `findMissingUtil(arr, ` `0` `, n - ` `1` `, diff);` `}` `// Driver Code` `public` `static` `void` `main (String[] args)` `{` ` ` `int` `arr[] = {` `2` `, ` `4` `, ` `8` `, ` `10` `, ` `12` `, ` `14` `};` ` ` `int` `n = arr.length;` ` ` `System.out.println(` `"The missing element is "` `+ ` ` ` `findMissing(arr, n));` `}` `}` `// This code is contributed by gurudev620gs.` |

## Python3

`# A Python3 program to find the missing` `# number in a given arithmetic progression` `import` `sys` `# A binary search based function` `# that returns the missing element in` `# arithmetic progression` `def` `findMissingUtil(arr, low, high, diff):` ` ` ` ` `while` `low <` `=` `high:` ` ` `# find index of middle element` ` ` `mid ` `=` `(low ` `+` `high)` `/` `/` `2` ` ` `# if mid == (nth position of element in AP)-1` ` ` `# the missing element will exist in right half` ` ` `if` `(arr[mid] ` `-` `arr[` `0` `])` `/` `/` `diff ` `=` `=` `mid:` ` ` `low ` `=` `mid ` `+` `1` ` ` `else` `:` ` ` `# the missing element will exist in left half` ` ` `high ` `=` `mid ` `-` `1` ` ` `# after breaking out of binary search loop` ` ` `# our missing element will exist between high and low` ` ` `# our missing element will be a[high] + commom difference` ` ` `# or a[low] - commom difference` ` ` `return` `arr[high] ` `+` `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 integer.` `def` `findMissing(arr, n):` ` ` `# If exactly one element is missing, then` ` ` `# we can find difference of arithmetic` ` ` `# progression using following formula.` ` ` `# Example, 2, 4, 6, 10, diff = (10-2)/4 = 2.` ` ` `# The assumption in formula is that the` ` ` `# difference is an integer.` ` ` `diff ` `=` `int` `((arr[n ` `-` `1` `] ` `-` `arr[` `0` `]) ` `/` `n);` ` ` `# Binary search for the missing number` ` ` `# using above calculated diff` ` ` `return` `findMissingUtil(arr, ` `0` `, n ` `-` `1` `, diff);` `# Driver Code` `arr ` `=` `[` `2` `, ` `4` `, ` `8` `, ` `10` `, ` `12` `, ` `14` `];` `n ` `=` `len` `(arr);` `print` `(` `"The missing element is"` `,` ` ` `findMissing(arr, n));` `# This code is contributed by gurudev620gs` |

## C#

`// A C# program to find` `// the missing number in` `// a given arithmetic` `// progression` `using` `System;` `class` `GFG` `{` ` ` `// A binary search function that` `// returns the missing` `// element in arithmetic` `// progression` `static` `int` `findMissingUtil(` `int` `[]arr, ` `int` `low,` ` ` `int` `high, ` `int` `diff)` `{` ` ` `// Find index of middle element` ` ` `int` `mid;` ` ` `while` `(low <= high)` ` ` `{ ` ` ` `// find index of middle element` ` ` `mid = (low + high) / 2;` ` ` `// if mid == (nth position of element in AP)-1` ` ` `// the missing element will exist in right half` ` ` `if` `((arr[mid] - arr[0]) / diff == mid)` ` ` `low = mid + 1;` ` ` `else` ` ` `// the missing element will exist in left half` ` ` `high = mid - 1;` ` ` `}` ` ` `// after breaking out of binary search loop` ` ` `// our missing element will exist between high and low` ` ` `// our missing element will be a[high] + commom difference` ` ` `// or a[low] - commom difference` ` ` `return` `arr[high] + 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 integer.` `static` `int` `findMissing(` `int` `[]arr, ` `int` `n)` `{` ` ` `// If exactly one element` ` ` `// is missing, then we can` ` ` `// find difference of arithmetic` ` ` `// progression using following` ` ` `// formula. Example, 2, 4, 6, 10,` ` ` `// diff = (10-2)/4 = 2.The assumption` ` ` `// in formula is that the difference` ` ` `// is an integer.` ` ` `int` `diff = (arr[n - 1] -` ` ` `arr[0]) / n;` ` ` `// Binary search for the` ` ` `// missing number using` ` ` `// above calculated diff` ` ` `return` `findMissingUtil(arr, 0,` ` ` `n - 1, diff);` `}` `// Driver Code` `public` `static` `void` `Main ()` `{` ` ` `int` `[]arr = {2, 4, 8,` ` ` `10, 12, 14};` ` ` `int` `n = arr.Length;` ` ` `Console.WriteLine(` `"The missing element is "` `+` ` ` `findMissing(arr, n));` `}` `}` `// This code is contributed by gurudev620gs.` |

## PHP

`<?php` `// A PHP program to find the missing` `// number in a given arithmetic progression` `// A binary search based function` `// that returns the missing element in` `// arithmetic progression` `function` `findMissingUtil(` `$arr` `, ` `$low` `, ` `$high` `, ` `$diff` `)` `{` ` ` `while` `(` `$low` `<= ` `$high` `)` ` ` `{ ` ` ` `// find index of middle element` ` ` `$mid` `= (` `$low` `+ ` `$high` `) / 2;` ` ` `// if mid == (nth position of element in AP)-1` ` ` `// the missing element will exist in right half` ` ` `if` `((` `$arr` `[` `$mid` `] - ` `$arr` `[0]) / ` `$diff` `== ` `$mid` `)` ` ` `$low` `= ` `$mid` `+ 1;` ` ` `else` ` ` `// the missing element will exist in left half` ` ` `$high` `= ` `$mid` `- 1;` ` ` `}` ` ` `// after breaking out of binary search loop` ` ` `// our missing element will exist between high and low` ` ` `// our missing element will be a[high] + commom difference` ` ` `// or a[low] - commom difference` ` ` `return` `$arr` `[` `$high` `] + ` `$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 integer.` `function` `findMissing(` `$arr` `, ` `$n` `)` `{` ` ` `// If exactly one element is missing, then` ` ` `// we can find difference of arithmetic` ` ` `// progression using following formula.` ` ` `// Example, 2, 4, 6, 10, diff = (10-2)/4 = 2.` ` ` `// The assumption in formula is that the` ` ` `// difference is an integer.` ` ` `$diff` `= (` `$arr` `[` `$n` `- 1] - ` `$arr` `[0]) / ` `$n` `;` ` ` `// Binary search for the missing number` ` ` `// using above calculated diff` ` ` `return` `findMissingUtil(` `$arr` `, 0, ` `$n` `- 1, ` `$diff` `);` `}` `// Driver Code` `$arr` `= ` `array` `(2, 4, 8, 10, 12, 14);` `$n` `= sizeof(` `$arr` `);` `echo` `"The missing element is "` `,` ` ` `findMissing(` `$arr` `, ` `$n` `);` `// This code is contributed by gurudev620.gs` `?>` |

## Javascript

`<script>` ` ` `// A JavaScript program to find` ` ` `// the missing number in` ` ` `// a given arithmetic` ` ` `// progression` ` ` ` ` `// A binary search function that` ` ` `// returns the missing` ` ` `// element in arithmetic` ` ` `// progression` ` ` `function` `findMissingUtil(arr, low, high, diff)` ` ` `{` ` ` `// Find index of middle element` ` ` `int mid;` ` ` `while` `(low <= high)` ` ` `{ ` ` ` `// find index of middle element` ` ` `mid = (low + high) / 2;` ` ` `// if mid == (nth position of element in AP)-1` ` ` `// the missing element will exist in right half` ` ` `if` `((arr[mid] - arr[0]) / diff == mid)` ` ` `low = mid + 1;` ` ` `else` ` ` `// the missing element will exist in left half` ` ` `high = mid - 1;` ` ` `}` ` ` `// after breaking out of binary search loop` ` ` `// our missing element will exist between high and low` ` ` `// our missing element will be a[high] + commom difference` ` ` `// or a[low] - commom difference` ` ` `return` `arr[high] + 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 integer.` ` ` `function` `findMissing(arr, n)` ` ` `{` ` ` `// If exactly one element is missing,` ` ` `// then we can find difference of` ` ` `// arithmetic progression using` ` ` `// following formula. Example, 2, 4,` ` ` `// 6, 10, diff = (10-2)/4 = 2.` ` ` `// The assumption in formula is that` ` ` `// the difference is an integer.` ` ` `let diff = parseInt((arr[n - 1] - arr[0]) / n, 10);` ` ` `// Binary search for the missing` ` ` `// number using above calculated diff` ` ` `return` `findMissingUtil(arr, 0, n - 1, diff);` ` ` `}` ` ` ` ` `let arr = [2, 4, 8, 10, 12, 14];` ` ` `let n = arr.length;` ` ` `document.write(` `"The missing element is "` `+ ` ` ` `findMissing(arr, n));` ` ` `</script>` |

**Output**

The missing element is 6

Thanks to gurudev620gs for suggesting the above solution.

**Exercise:**

Solve the same problem for Geometrical Series. What is the time complexity of your solution? What about Fibonacci Series?

This article is contributed by **Harshit Agrawal**. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**

In case you wish to attend **live classes **with experts, please refer **DSA Live Classes for Working Professionals **and **Competitive Programming Live for Students**.