Find the smallest missing number

Given a sorted array of n distinct integers where each integer is in the range from 0 to m-1 and m > n. Find the smallest number that is missing from the array.

Examples
Input: {0, 1, 2, 6, 9}, n = 5, m = 10
Output: 3

Input: {4, 5, 10, 11}, n = 4, m = 12
Output: 0

Input: {0, 1, 2, 3}, n = 4, m = 5
Output: 4

Input: {0, 1, 2, 3, 4, 5, 6, 7, 10}, n = 9, m = 11
Output: 8

Thanks to Ravichandra for suggesting following two methods.

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

Method 1 (Use Binary Search)
For i = 0 to m-1, do binary search for i in the array. If i is not present in the array then return i.

Time Complexity: O(m log n)

Method 2 (Linear Search)
If arr is not 0, return 0. Otherwise traverse the input array starting from index 0, and for each pair of elements a[i] and a[i+1], find the difference between them. if the difference is greater than 1 then a[i]+1 is the missing number.

Time Complexity: O(n)

Method 3 (Use Modified Binary Search)
Thanks to yasein and Jams for suggesting this method.
In the standard Binary Search process, the element to be searched is compared with the middle element and on the basis of comparison result, we decide whether to search is over or to go to left half or right half.
In this method, we modify the standard Binary Search algorithm to compare the middle element with its index and make decision on the basis of this comparison.

…1) If the first element is not same as its index then return first index
…2) Else get the middle index say mid
…………a) If arr[mid] greater than mid then the required element lies in left half.
…………b) Else the required element lies in right half.

C++

 // C++ program to find the smallest elements // missing in a sorted array. #include using namespace std;    int findFirstMissing(int array[],                      int start, int end) {     if (start > end)         return end + 1;        if (start != array[start])         return start;        int mid = (start + end) / 2;        // Left half has all elements      // from 0 to mid     if (array[mid] == mid)         return findFirstMissing(array,                              mid+1, end);        return findFirstMissing(array, start, mid); }    // Driver code int main() {     int arr[] = {0, 1, 2, 3, 4, 5, 6, 7, 10};     int n = sizeof(arr)/sizeof(arr);     cout << "Smallest missing element is " <<         findFirstMissing(arr, 0, n-1) << endl; }    // This code is contributed by // Shivi_Aggarwal

C

 // C program to find the smallest elements missing // in a sorted array. #include    int findFirstMissing(int array[], int start, int end) {     if (start  > end)         return end + 1;        if (start != array[start])         return start;        int mid = (start + end) / 2;        // Left half has all elements from 0 to mid     if (array[mid] == mid)         return findFirstMissing(array, mid+1, end);        return findFirstMissing(array, start, mid); }    // driver program to test above function int main() {     int arr[] = {0, 1, 2, 3, 4, 5, 6, 7, 10};     int n = sizeof(arr)/sizeof(arr);     printf("Smallest missing element is %d",            findFirstMissing(arr, 0, n-1));     return 0; }

Java

 class SmallestMissing  {     int findFirstMissing(int array[], int start, int end)      {         if (start > end)             return end + 1;            if (start != array[start])             return start;            int mid = (start + end) / 2;            // Left half has all elements from 0 to mid         if (array[mid] == mid)             return findFirstMissing(array, mid+1, end);            return findFirstMissing(array, start, mid);     }        // Driver program to test the above function     public static void main(String[] args)      {         SmallestMissing small = new SmallestMissing();         int arr[] = {0, 1, 2, 3, 4, 5, 6, 7, 10};         int n = arr.length;         System.out.println("First Missing element is : "                 + small.findFirstMissing(arr, 0, n - 1));     } }

Python3

 # Python3 program to find the smallest # elements missing in a sorted array.    def findFirstMissing(array, start, end):        if (start > end):         return end + 1        if (start != array[start]):         return start;        mid = int((start + end) / 2)        # Left half has all elements     # from 0 to mid     if (array[mid] == mid):         return findFirstMissing(array,                           mid+1, end)        return findFirstMissing(array,                            start, mid)       # driver program to test above function arr = [0, 1, 2, 3, 4, 5, 6, 7, 10] n = len(arr) print("Smallest missing element is",       findFirstMissing(arr, 0, n-1))    # This code is contributed by Smitha Dinesh Semwal

C#

 // C# program to find the smallest // elements missing in a sorted array. using System;    class GFG {     static int findFirstMissing(int []array,                             int start, int end)      {         if (start > end)             return end + 1;            if (start != array[start])             return start;            int mid = (start + end) / 2;            // Left half has all elements from 0 to mid         if (array[mid] == mid)             return findFirstMissing(array, mid+1, end);            return findFirstMissing(array, start, mid);     }        // Driver program to test the above function     public static void Main()      {         int []arr = {0, 1, 2, 3, 4, 5, 6, 7, 10};         int n = arr.Length;                    Console.Write("smallest Missing element is : "                     + findFirstMissing(arr, 0, n - 1));     } }    // This code is contributed by Sam007

PHP

 \$end)         return \$end + 1;        if (\$start != \$array[\$start])         return \$start;        \$mid = (\$start + \$end) / 2;        // Left half has all      // elements from 0 to mid     if (\$array[\$mid] == \$mid)         return findFirstMissing(\$array,                                  \$mid + 1,                                  \$end);        return findFirstMissing(\$array,                              \$start,                              \$mid); }        // Driver Code     \$arr = array (0, 1, 2, 3, 4, 5, 6, 7, 10);     \$n = count(\$arr);     echo "Smallest missing element is " ,           findFirstMissing(\$arr, 2, \$n - 1);            // This code Contributed by Ajit. ?>

Output:

Smallest missing element is 8

Note: This method doesn’t work if there are duplicate elements in the array.

Time Complexity: O(Logn)

Please write comments if you find the above codes/algorithms incorrect, or find other ways to solve the same problem.

My Personal Notes arrow_drop_up

Improved By : jit_t, Shivi_Aggarwal

Article Tags :
Practice Tags :

8

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