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

C++Server Side ProgrammingProgramming

Concept

With respect of a given array of unique integers where each integer of the given array lies in the range [1, N], the size of array is (N-4) and no single element is repeated. So, four numbers, from 1 to N, are missing in the array. Determine the 4 missing numbers in sorted order.

Input

arr[] = {3, 6, 7, 4, 10}

Output

1 2 5 8 9

Input

arr[] = { 2, 8, 4, 13, 6, 11, 9, 5, 10 }

Output

1 3 7 12

Method

Now a simple O(N) solution is to implement an auxiliary array of size N to indicate or markvisited elements. Visit the input array and indicate or mark elements in the auxiliary array. At last print all those indexes that are not indicated or marked.

Now the question is arisen that how to solve with O(1) auxiliary space?

At first we initialize an array named helper of length 4 in order to compensate the 4 missing numbers and fill them with zero. After that we iterate from i=0 to i < length_of_array of the given array and accept the absolute of the of the i-th element and store it in a variable named temp. In this time, we will verify −

• It has been seen that if the element’s absolute value is smaller than the length of the input array then we will multiply the array[temp] element with -1 (in order to indicate or mark the visited element).

• It has been seen that if the element’s absolute value is larger than the length of the input array then we will put the value of helper[temp%array.length] element with -1 (in order to indicate or mark the visited element).

Now we iterate over input array and those index whose value is still larger than zero then those elements were not encountered in the input array. As a result of this we print the (index+1) value of the index whose element is larger than zero.

Now we will iterate over helper array and those index whose value is still larger than zero then those elements were not encountered in the input array. As a result of this weprint the (index+array.length+1) value of the index whose element is larger than zero.

Example

Live Demo

// CPP program to find missing 4 elements
// in an array of size N where elements are
// in range from 1 to N+4.
#include <bits/stdc++.h>
using namespace std;
// Used to find missing 4 numbers in O(N) time
// and O(1) auxiliary space.
void missing4(int arr1[], int n1){
// Used to keep track of 4 possible numbers
// greater than length of input array
// In case of Java, helper is automatically
// initialized as 0.
int helper[4];
// Visit the input array and mark
// visited elements either by marking
// them as negative in arr[] or in
// helper[].
for (int i = 0; i < n1; i++) {
int temp1 = abs(arr1[i]);
// It has been seen that if element is smaller than or
// equal to length, mark its
// presence in arr1[]
if (temp1 <= n1)
arr1[temp1 - 1] *= (-1);
// Indicate or mark presence in helper[]
else if (temp1 > n1) {
if (temp1 % n1 != 0)
helper[temp1 % n1 - 1] = -1;
else
helper[(temp1 % n1) + n1 - 1] = -1;
}
}
// Used to print all those elements whose presence
// is not marked.
for (int i = 0; i < n1; i++)
if (arr1[i] > 0)
cout << (i + 1) << " ";
for (int i = 0; i < 4; i++)
if (helper[i] >= 0)
cout << (n1 + i + 1) << " ";
return;
}
// Driver code
int main(){
int arr1[] = { 2, 8, 4, 13, 6, 11, 9, 5, 10 };
int n1 = sizeof(arr1) / sizeof(arr1[0]);
missing4(arr1, n1);
return 0;
}

Output

1 3 7 12
Published on 24-Jul-2020 11:06:41