Open In App

Javascript Program for 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.

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[0] 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.




<script>
 
    // Javascript program to find the smallest
    // elements missing in a sorted array.
     
    function findFirstMissing(array, start, end)
    {
        if (start > end)
            return end + 1;
   
        if (start != array[start])
            return start;
   
        let mid = parseInt((start + end) / 2, 10);
   
        // Left half has all elements from 0 to mid
        if (array[mid] == mid)
            return findFirstMissing(array, mid+1, end);
   
        return findFirstMissing(array, start, mid);
    }
     
    let arr = [0, 1, 2, 3, 4, 5, 6, 7, 10];
    let n = arr.length;
 
    document.write("smallest Missing element is " +
    findFirstMissing(arr, 0, n - 1));
     
</script>

Output
Smallest missing element is 8

Note: This method doesn’t work if there are duplicate elements in the array.
Time Complexity: O(Logn)
Auxiliary Space: O(1)

Another Method: The idea is to use Recursive Binary Search to find the smallest missing number. Below is the illustration with the help of steps:

Below is the implementation of the above approach:




<script>
 
// Javascript program for the above approach
 
// Program to find missing element
function findFirstMissing(arr, start, end, first)
{
    if (start < end)
    {
        let mid = (start + end) / 2;
     
        /** Index matches with value
        at that index, means missing
        element cannot be upto that point*/
        if (arr[mid] != mid + first)
            return findFirstMissing(arr, start,
                                    mid, first);
        else
            return findFirstMissing(arr, mid + 1,
                                    end, first);
    }
    return start + first;
}
 
// Program to find Smallest
// Missing in Sorted Array
function findSmallestMissinginSortedArray(arr)
{
     
    // Check if 0 is missing
    // in the array
    if (arr[0] != 0)
        return 0;
     
    // Check is all numbers 0 to n - 1
    // are present in array
    if (arr[arr.length - 1] == arr.length - 1)
        return arr.length;
     
    let first = arr[0];
     
    return findFirstMissing(
        arr, 0, arr.length - 1, first);
}
 
// Driver code
let arr = [ 0, 1, 2, 3, 4, 5, 7 ];
let n = arr.length;
 
// Function Call
document.write("First Missing element is : " +
     findSmallestMissinginSortedArray(arr));
 
// This code is contributed by Mayank Tyagi
 
</script>

Output
First Missing element is : 6

Time Complexity: O(LogN) 
Auxiliary Space: O(logN) where logN is the size of the recursive stack tree

Please refer complete article on Find the smallest missing number for more details!


Article Tags :