Find the Missing Number in a sorted array

Given a list of n-1 integers and these integers are in the range of 1 to n. There are no duplicates in list. One of the integers is missing in the list. Write an efficient code to find the missing integer.

Examples:

Input : arr[] = [1, 2, 3, 4, 6, 7, 8]
Output : 5

Input : arr[] = [1, 2, 3, 4, 5, 6, 8, 9]
Output : 7

One Simple solution is to apply methods discussed for finding the missing element in an unsorted array. Time complexity of this solution is O(n).

An efficient solution is based on the divide and conquer algorithm that we have seen in binary search, the concept behind this solution is that the elements appearing before the missing element will have ar[i] – i = 1 and those appearing after the missing element will have ar[i] – i = 2.

This solution has a time complexity of O(log n)

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// A binary search based program to find the
// only missing number in a sorted array of
// distinct elements within limited range.
#include <iostream>
using namespace std;
  
int search(int ar[], int size)
{
    int a = 0, b = size - 1;
    int mid;
    while ((b - a) > 1) {
        mid = (a + b) / 2;
        if ((ar[a] - a) != (ar[mid] - mid))
            b = mid;
        else if ((ar[b] - b) != (ar[mid] - mid))
            a = mid;
    }
    return (ar[mid] + 1);
}
  
int main()
{
    int ar[] = { 1, 2, 3, 4, 5, 6, 8 };
    int size = sizeof(ar) / sizeof(ar[0]);
    cout << "Missing number:" << search(ar, size);
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// A binary search based program 
// to find the only missing number
// in a sorted array of distinct 
// elements within limited range.
import java.io.*;
  
class GFG 
{
static int search(int ar[], 
                  int size)
{
    int a = 0, b = size - 1;
    int mid = 0;
    while ((b - a) > 1)
    {
        mid = (a + b) / 2;
        if ((ar[a] - a) != (ar[mid] - mid))
            b = mid;
        else if ((ar[b] - b) != (ar[mid] - mid))
            a = mid;
    }
    return (ar[mid] + 1);
}
  
// Driver Code
public static void main (String[] args) 
{
    int ar[] = { 1, 2, 3, 4, 5, 6, 8 };
    int size = ar.length;
    System.out.println("Missing number: " +
                        search(ar, size));
}
}
  
// This code is contributed 
// by inder_verma.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# A binary search based program to find 
# the only missing number in a sorted 
# in a sorted array of distinct elements 
# within limited range
def search(ar, size):
    a = 0
    b = size - 1
    mid = 0
    while b > a + 1:
        mid = (a + b) // 2
        if (ar[a] - a) != (ar[mid] - mid):
            b = mid
        elif (ar[b] - b) != (ar[mid] - mid):
            a = mid
    return ar[mid] + 1
  
# Driver Code
a = [1, 2, 3, 4, 5, 6, 8]
n = len(a)
  
print("Missing number:", search(a, n))
  
# This code is contributed
# by Mohit Kumar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// A binary search based program 
// to find the only missing number
// in a sorted array of distinct 
// elements within limited range.
using System;
  
class GFG 
{
static int search(int []ar, 
                  int size)
{
    int a = 0, b = size - 1;
    int mid = 0;
    while ((b - a) > 1)
    {
        mid = (a + b) / 2;
        if ((ar[a] - a) != (ar[mid] - mid))
            b = mid;
        else if ((ar[b] - b) != (ar[mid] - mid))
            a = mid;
    }
    return (ar[mid] + 1);
}
  
// Driver Code
static public void Main (String []args) 
{
    int []ar = { 1, 2, 3, 4, 5, 6, 8 };
    int size = ar.Length;
    Console.WriteLine("Missing number: " +
                        search(ar, size));
}
}
  
// This code is contributed 
// by Arnab Kundu

chevron_right


PHP

1)
{
$mid = (int)(($a + $b) / 2);
if (($ar[$a] – $a) != ($ar[$mid] –
$mid))
$b = $mid;
else if (($ar[$b] – $b) != ($ar[$mid] –
$mid))
$a = $mid;
}
return ($ar[$mid] + 1);
}

// Driver Code
$ar = array(1, 2, 3, 4, 5, 6, 8 );
$size = sizeof($ar);
echo “Missing number: “,
search($ar, $size);

// This code is contributed by ajit.
?>


Output:

Missing number: 7


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.