Find all missing numbers from a given sorted array

Given a sorted array arr[] of N integers, The task is to find the multiple missing elements in the array between the ranges [arr[0], arr[N-1]].

Examples:

Input: arr[] = {6, 7, 10, 11, 13}
Output: 8 9 12 
Explanation: 
The elements of the array are present in the range of the maximum and minimum array element [6, 13]. Therefore, the total values will be {6, 7, 8, 9, 10, 11, 12, 13}. 
The elements from the above range which are missing from the array are {8, 9, 12}. 
 

Input: arr[] = {1, 2, 4, 6}
Output: 3 5

Naive Approach: The naive idea is to iterate over the difference between the consecutive pair of elements and the print all the numbers in this range if the difference is non-zero. Below are the steps:

  1. Initialize the variable diff which is equal to arr[0] – 0.
  2. Now traverse the array and see if the difference between arr[i] – i and diff is zero or not.
  3. If the difference is not equal to zero in the above steps, then the missing element is found.
  4. To find the multiple missing elements run a loop inside it and see if the diff is less than arr[i] – i then print the missing element i.e., i + diff.
  5. Now increment the diff as the difference is increased now.
  6. Repeat from step 2 until all the missing numbers are not found.

Below is the implementation of the above approach:



C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the missing elements
void printMissingElements(int arr[], int N)
{
 
    // Initialize diff
    int diff = arr[0] - 0;
 
    for (int i = 0; i < N; i++) {
 
        // Check if diff and arr[i]-i
        // both are equal or not
        if (arr[i] - i != diff) {
 
            // Loop for consecutive
            // missing elements
            while (diff < arr[i] - i) {
                cout << i + diff << " ";
                diff++;
            }
        }
    }
}
 
// Driver Code
int main()
{
    // Given array arr[]
    int arr[] = { 6, 7, 10, 11, 13 };
 
    int N = sizeof(arr) / sizeof(int);
 
    // Function Call
    printMissingElements(arr, N);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for the above approach
import java.util.*;
 
class GFG{
     
// Function to find the missing elements
static void printMissingElements(int arr[],
                                 int N)
{
 
    // Initialize diff
    int diff = arr[0] - 0;
 
    for(int i = 0; i < N; i++)
    {
 
        // Check if diff and arr[i]-i
        // both are equal or not
        if (arr[i] - i != diff)
        {
 
            // Loop for consecutive
            // missing elements
            while (diff < arr[i] - i)
            {
                System.out.print((i + diff) + " ");
                diff++;
            }
        }
    }
}
 
// Driver Code
public static void main (String[] args)
{
     
    // Given array arr[]
    int arr[] = { 6, 7, 10, 11, 13 };
     
    int N = arr.length;
     
    // Function call
    printMissingElements(arr, N);
}
}
 
// This code is contributed by offbeat

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program for the above approach
 
# Function to find the missing elements
def printMissingElements(arr, N):
 
    # Initialize diff
    diff = arr[0]
 
    for i in range(N):
 
        # Check if diff and arr[i]-i
        # both are equal or not
        if(arr[i] - i != diff):
 
            # Loop for consecutive
            # missing elements
            while(diff < arr[i] - i):
                print(i + diff, end = " ")
                diff += 1
 
# Driver Code
 
# Given array arr[]
arr = [ 6, 7, 10, 11, 13 ]
 
N = len(arr)
 
# Function call
printMissingElements(arr, N)
 
# This code is contributed by Shivam Singh

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program for the above approach
using System;
 
class GFG{
     
// Function to find the missing elements
static void printMissingElements(int[] arr,
                                 int N)
{
     
    // Initialize diff
    int diff = arr[0] - 0;
 
    for(int i = 0; i < N; i++)
    {
         
        // Check if diff and arr[i]-i
        // both are equal or not
        if (arr[i] - i != diff)
        {
             
            // Loop for consecutive
            // missing elements
            while (diff < arr[i] - i)
            {
                Console.Write(i + diff + " ");
                diff++;
            }
        }
    }
}
 
// Driver code
static void Main()
{
     
    // Given array arr[]
    int[] arr = { 6, 7, 10, 11, 13 };
     
    int N = arr.Length;
     
    // Function call
    printMissingElements(arr, N);
}
}
 
// This code is contributed by divyeshrabadiya07

chevron_right


Output: 

8 9 12


 

Time Complexity: O(N2)
Auxiliary Space: O(1)

Efficient Approach: The idea is to use Hashing to optimize the above approach. Create a boolean array(say b[]) of size equal to the maximum element in the array and mark only those positions in the array b[] which are present in the given array. Print all the index in the array b[] that are not marked. 
Below are the steps:  

  1. Initialize a boolean array b[] with zero of size equals to the maximum element of the array.
  2. Iterate over the given array and mark for each element in the given array mark that index as true in the array b[].
  3. Now traverse the given array b[] from index arr[0] and print those index whose value is false as they are the element that is missing in the given array.

Below is the implementation of the above approach:
 

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the missing elements
void printMissingElements(int arr[], int N)
{
    // Initialize an array with zero
    // of size equals to the maximum
    // element in the array
    int b[arr[N - 1] + 1] = { 0 };
 
    // Make b[i]=1 if i is present
    // in the array
    for (int i = 0; i < N; i++) {
 
        // If the element is present
        // make b[arr[i]]=1
        b[arr[i]] = 1;
    }
 
    // Print the indices where b[i]=0
    for (int i = arr[0]; i <= arr[N - 1]; i++) {
 
        if (b[i] == 0) {
            cout << i << " ";
        }
    }
}
 
// Driver Code
int main()
{
    // Given array arr[]
    int arr[] = { 6, 7, 10, 11, 13 };
 
    int N = sizeof(arr) / sizeof(int);
 
    // Function Call
    printMissingElements(arr, N);
 
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for the above approach
import java.util.*;
 
class GFG{
     
// Function to find the missing elements
static void printMissingElements(int arr[],
                                int N)
{
     
    // Initialize an array with zero
    // of size equals to the maximum
    // element in the array
    int[] b = new int[arr[N - 1] + 1];
 
    // Make b[i]=1 if i is present
    // in the array
    for(int i = 0; i < N; i++)
    {
         
        // If the element is present
        // make b[arr[i]]=1
        b[arr[i]] = 1;
    }
 
    // Print the indices where b[i]=0
    for(int i = arr[0]; i <= arr[N - 1]; i++)
    {
        if (b[i] == 0)
        {
            System.out.print(i + " ");
        }
    }
}
 
// Driver Code
public static void main (String[] args)
{
     
    // Given array arr[]
    int arr[] = { 6, 7, 10, 11, 13 };
     
    int N = arr.length;
     
    // Function call
    printMissingElements(arr, N);
}
}
 
// This code is contributed by offbeat

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to implement
# the above approach
 
# Function to find the missing elements
def printMissingElements(arr, N):
 
    # Initialize an array with zero
    # of size equals to the maximum
    # element in the array
    b = [0] * (arr[N - 1] + 1)
 
    # Make b[i]=1 if i is present
    # in the array
    for i in range(N):
 
        # If the element is present
        # make b[arr[i]]=1
        b[arr[i]] = 1
 
    # Print the indices where b[i]=0
    for i in range(arr[0], arr[N - 1] + 1):
        if(b[i] == 0):
            print(i, end = " ")
 
# Driver Code
 
# Given array arr[]
arr = [ 6, 7, 10, 11, 13 ]
 
N = len(arr)
 
# Function call
printMissingElements(arr, N)
 
# This code is contributed by Shivam Singh

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program for
// the above approach
using System;
class GFG{
     
// Function to find the missing elements
static void printMissingElements(int []arr,
                                 int N)
{    
  // Initialize an array with zero
  // of size equals to the maximum
  // element in the array
  int[] b = new int[arr[N - 1] + 1];
 
  // Make b[i]=1 if i is present
  // in the array
  for(int i = 0; i < N; i++)
  {
    // If the element is present
    // make b[arr[i]]=1
    b[arr[i]] = 1;
  }
 
  // Print the indices where b[i]=0
  for(int i = arr[0]; i <= arr[N - 1];
          i++)
  {
    if (b[i] == 0)
    {
      Console.Write(i + " ");
    }
  }
}
 
// Driver Code
public static void Main(String[] args)
{
  // Given array []arr
  int []arr = {6, 7, 10, 11, 13};
 
  int N = arr.Length;
 
  // Function call
  printMissingElements(arr, N);
}
}
 
// This code is contributed by Princi Singh

chevron_right


Output: 

8 9 12


 

Time Complexity: O(M), where M is the maximum element of the array. 
Auxiliary Space: O(M)

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

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.