# Find the only missing number in a sorted array

You are given a sorted array of N integers from 1 to N with one number missing find the missing number Expected time complexity O(logn)
Examples:

```Input :ar[] = {1, 3, 4, 5}
Output : 2

Input : ar[] = {1, 2, 3, 4, 5, 7, 8}
Output : 6
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

A simple solution is to linearly traverse the given array. Find the point where current element is not one more than previous.

An efficient solution is to use binary search. We use the index to search for the missing element and modified binary search. If element at mid != index+1 and this is first missing element then mid + 1 is the missing element. Else if this is not first missing element but ar[mid] != mid+1 search in left half. Else search in right half and if left>right then no element is missing.

## C++

 `// CPP program to find the only missing element. ` `#include ` `using` `namespace` `std; ` ` `  `int` `findmissing(``int` `ar[], ``int` `N) ` `{ ` `    ``int` `l = 0, r = N - 1; ` `    ``while` `(l <= r) { ` ` `  `        ``int` `mid = (l + r) / 2; ` ` `  `        ``// If this is the first element  ` `        ``// which is not index + 1, then  ` `        ``// missing element is mid+1 ` `        ``if` `(ar[mid] != mid + 1 &&  ` `                        ``ar[mid - 1] == mid) ` `            ``return` `mid + 1; ` ` `  `        ``// if this is not the first missing  ` `        ``// element search in left side ` `        ``if` `(ar[mid] != mid + 1) ` `            ``r = mid - 1; ` ` `  `        ``// if it follows index+1 property then  ` `        ``// search in right side ` `        ``else` `            ``l = mid + 1; ` `    ``} ` ` `  `    ``// if no element is missing ` `    ``return` `-1; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = {1, 2, 3, 4, 5, 7, 8}; ` `    ``int` `N = ``sizeof``(arr)/``sizeof``(arr); ` `    ``cout << findmissing(arr, N); ` `    ``return` `0; ` `} `

## Java

 `// Java program to find  ` `// the only missing element. ` `class` `GFG ` `{ ` `static` `int` `findmissing(``int` `[] ar, ``int` `N) ` `{ ` `     `  `    ``int` `l = ``0``, r = N - ``1``; ` `    ``while` `(l <= r)  ` `    ``{ ` `        ``int` `mid = (l + r) / ``2``; ` `     `  `        ``// If this is the first element  ` `        ``// which is not index + 1, then  ` `        ``// missing element is mid+1 ` `        ``if` `(ar[mid] != mid + ``1` `&&  ` `            ``ar[mid - ``1``] == mid) ` `            ``return` `(mid + ``1``); ` `     `  `        ``// if this is not the first ` `        ``// missing element search ` `        ``// in left side ` `        ``if` `(ar[mid] != mid + ``1``) ` `            ``r = mid - ``1``; ` `     `  `        ``// if it follows index+1 ` `        ``// property then search ` `        ``// in right side ` `        ``else` `            ``l = mid + ``1``; ` `    ``} ` ` `  `// if no element is missing ` `return` `-``1``; ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String [] args) ` `{ ` `    ``int` `arr[] = {``1``, ``2``, ``3``, ``4``, ``5``, ``7``, ``8``}; ` `    ``int` `N = arr.length; ` `    ``System.out.println(findmissing(arr, N)); ` `} ` `} ` ` `  `// This code is contributed  ` `// by Shivi_Aggarwal `

## Python3

 `# PYTHON 3 program to find  ` `# the only missing element. ` `def` `findmissing(ar, N): ` `    ``l ``=` `0` `    ``r ``=` `N ``-` `1` `    ``while` `(l <``=` `r):  ` `        ``mid ``=` `(l ``+` `r) ``/` `2` `        ``mid``=` `int` `(mid) ` ` `  `    ``# If this is the first element  ` `    ``# which is not index + 1, then  ` `    ``# missing element is mid+1 ` `        ``if``(ar[mid] !``=` `mid ``+` `1` `and` `           ``ar[mid ``-` `1``] ``=``=` `mid): ` `            ``return` `(mid ``+` `1``) ` ` `  `    ``# if this is not the first  ` `    ``# missing element search  ` `    ``# in left side ` `        ``elif``(ar[mid] !``=` `mid ``+` `1``): ` `            ``r ``=` `mid ``-` `1` ` `  `    ``# if it follows index+1  ` `    ``# property then search  ` `    ``# in right side ` `        ``else``: ` `            ``l ``=` `mid ``+` `1` `     `  `    ``# if no element is missing ` `    ``return` `(``-``1``) ` `     `  `def` `main(): ` `    ``ar``=` `[``1``, ``2``, ``3``, ``4``, ``5``, ``7``, ``8``] ` `    ``N ``=` `len``(ar) ` `    ``res``=` `findmissing(ar, N) ` `    ``print` `(res) ` `if` `__name__ ``=``=` `"__main__"``: ` `    ``main() ` ` `  `# This code is contributed  ` `# by Shivi_Aggarwal `

## C#

 `// C# program to find  ` `// the only missing element. ` `using` `System; ` ` `  `class` `GFG ` `{ ` `static` `int` `findmissing(``int` `[]ar,  ` `                       ``int` `N) ` `{ ` `     `  `    ``int` `l = 0, r = N - 1; ` `    ``while` `(l <= r)  ` `    ``{ ` `        ``int` `mid = (l + r) / 2; ` `     `  `        ``// If this is the first element  ` `        ``// which is not index + 1, then  ` `        ``// missing element is mid+1 ` `        ``if` `(ar[mid] != mid + 1 &&  ` `            ``ar[mid - 1] == mid) ` `            ``return` `(mid + 1); ` `     `  `        ``// if this is not the first ` `        ``// missing element search ` `        ``// in left side ` `        ``if` `(ar[mid] != mid + 1) ` `            ``r = mid - 1; ` `     `  `        ``// if it follows index+1 ` `        ``// property then search ` `        ``// in right side ` `        ``else` `            ``l = mid + 1; ` `    ``} ` ` `  `// if no element is missing ` `return` `-1; ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main() ` `{ ` `    ``int` `[]arr = {1, 2, 3, 4, 5, 7, 8}; ` `    ``int` `N = arr.Length; ` `    ``Console.WriteLine(findmissing(arr, N)); ` `} ` `} ` ` `  `// This code is contributed  ` `// by Akanksha Rai(Abby_akku) `

## PHP

 `  `

Output:

```6
```

Time Complexity:O(Log n)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.