Skip to content
Related Articles

Related Articles

Find the only missing number in a sorted array
  • Difficulty Level : Easy
  • Last Updated : 21 Aug, 2018

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




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


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




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


Output:

6

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

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.




My Personal Notes arrow_drop_up
Recommended Articles
Page :