Open In App
Related Articles

Find four missing numbers in an array containing elements from 1 to N

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Given an array of unique integers where each integer of the given array lies in the range [1, N]. The size of array is (N-4). No Single element is repeated. Hence four numbers from 1 to N are missing in the array. Find the 4 missing numbers in sorted order.

Examples:  

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

Input : arr[] = {1, 7, 3, 13, 5, 10, 8, 4, 9}
Output : 2 6 11 12

A simple O(N) solution is to use an auxiliary array of size N to mark visited elements. Traverse the input array and mark elements in the auxiliary array. Finally print all those indexes that are not marked. 

How to solve with O(1) auxiliary space? 
We initialize an array named helper of length 4 in order to compensate the 4 missing numbers and fill them with zero. Then we iterate from i=0 to i < length_of_array of the given array and take the Absolute of the i-th element and store it in a variable named temp

Now we will check: 

  1. If the element’s absolute value is less than the length of the input array then we will multiply the array[temp] element with -1 (in order to mark the visited element).
  2. If the element’s absolute value is greater than the length of the input array then we will put the value of helper[temp%array.length] element with -1 (in order to mark the visited element).

Then we iterate over input array and those index whose value is still greater than zero then those elements were not encountered in the input array. So we print the (index+1) value of the index whose element is greater than zero. 

Then we will iterate over helper array and those index whose value is still greater than zero then those elements were not encountered in the input array. So we print the (index+array.length+1) value of the index whose element is greater than zero.

Implementation:

C++


                    

Java

// Java program to find missing 4 elements
// in an array of size N where elements are
// in range from 1 to N+4.
class Missing4 {
 
    // Finds missing 4 numbers in O(N) time
    // and O(1) auxiliary space.
    public static void missing4(int[] arr)
    {
        // To keep track of 4 possible numbers
        // greater than length of input array
        // In Java, helper is automatically
        // initialized as 0.
        int[] helper = new int[4];
 
        // Traverse the input array and mark
        // visited elements either by marking
        // them as negative in arr[] or in
        // helper[].
        for (int i = 0; i < arr.length; i++) {
            int temp = Math.abs(arr[i]);
 
            // If element is smaller than or
            // equal to length, mark its
            // presence in arr[]
            if (temp <= arr.length)
                arr[temp - 1] *= (-1);
 
             // Mark presence in helper[]
             else if (temp > arr.length) {
                if (temp % arr.length != 0)
                    helper[temp % arr.length - 1] = -1;
                else
                    helper[(temp % arr.length) +
                                   arr.length - 1] = -1;
            }
        }
 
        // Print all those elements whose presence
        // is not marked.  
        for (int i = 0; i < arr.length; i++)
            if (arr[i] > 0)
                System.out.print(i + 1 + " ");     
        for (int i = 0; i < helper.length; i++)
            if (helper[i] >= 0)
                System.out.print(arr.length + i + 1 + " ");           
         
        return;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int[] arr = { 1, 7, 3, 12, 5, 10, 8, 4, 9 };
        missing4(arr);
    }
}

                    

Python3


                    

C#


                    

PHP


                    

Javascript


                    

Output
2 6 11 13 

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

 



Last Updated : 28 Sep, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads