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

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++



filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to find the only missing element.
#include <iostream>
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[0]);
    cout << findmissing(arr, N);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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)

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find 
// the only missing element. 
function findmissing(&$ar, $N)
{
    $r = $N - 1;
    $l = 0;
    while ($l <= $r)
    {
        $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
$ar = array(1, 2, 3, 4, 5, 7, 8);
$N = sizeof($ar);
echo(findmissing($ar, $N));
  
// This code is contributed 
// by Shivi_Aggarwal
?> 

chevron_right


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.