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:
- 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).
- 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++
#include <bits/stdc++.h>
using namespace std;
void missing4( int arr[], int n)
{
int helper[4];
for ( int i = 0; i < n; i++) {
int temp = abs (arr[i]);
if (temp <= n)
arr[temp - 1] *= (-1);
else if (temp > n) {
if (temp % n != 0)
helper[temp % n - 1] = -1;
else
helper[(temp % n) + n - 1] = -1;
}
}
for ( int i = 0; i < n; i++)
if (arr[i] > 0)
cout << (i + 1) << " " ;
for ( int i = 0; i < 4; i++)
if (helper[i] >= 0)
cout << (n + i + 1) << " " ;
return ;
}
int main()
{
int arr[] = { 1, 7, 3, 12, 5, 10, 8, 4, 9 };
int n = sizeof (arr) / sizeof (arr[0]);
missing4(arr, n);
return 0;
}
|
Java
class Missing4 {
public static void missing4( int [] arr)
{
int [] helper = new int [ 4 ];
for ( int i = 0 ; i < arr.length; i++) {
int temp = Math.abs(arr[i]);
if (temp <= arr.length)
arr[temp - 1 ] *= (- 1 );
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 ;
}
}
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 ;
}
public static void main(String[] args)
{
int [] arr = { 1 , 7 , 3 , 12 , 5 , 10 , 8 , 4 , 9 };
missing4(arr);
}
}
|
Python3
def missing4( arr) :
helper = [ 0 ] * 4
for i in range ( 0 , len (arr)) :
temp = abs (arr[i])
if (temp < = len (arr)) :
arr[temp - 1 ] = arr[temp - 1 ] * ( - 1 )
elif (temp > len (arr)) :
if (temp % len (arr)) :
helper[temp % len (arr) - 1 ] = - 1
else :
helper[(temp % len (arr)) + len (arr) - 1 ] = - 1
for i in range ( 0 , len (arr) ) :
if (arr[i] > 0 ) :
print ((i + 1 ) , end = " " )
for i in range ( 0 , len (helper)) :
if (helper[i] > = 0 ) :
print (( len (arr) + i + 1 ) , end = " " )
arr = [ 1 , 7 , 3 , 12 , 5 , 10 , 8 , 4 , 9 ]
missing4(arr)
|
C#
using System;
class Missing4 {
public static void missing4( int [] arr)
{
int [] helper = new int [4];
for ( int i = 0; i < arr.Length; i++) {
int temp = Math.Abs(arr[i]);
if (temp <= arr.Length)
arr[temp - 1] *= (-1);
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;
}
}
for ( int i = 0; i < arr.Length; i++)
if (arr[i] > 0)
Console.Write(i + 1 + " " );
for ( int i = 0; i < helper.Length; i++)
if (helper[i] >= 0)
Console.Write(arr.Length + i + 1 + " " );
return ;
}
public static void Main()
{
int [] arr = { 1, 7, 3, 12, 5, 10, 8, 4, 9 };
missing4(arr);
}
}
|
PHP
<?php
function missing4( $arr , $n )
{
$helper = array (0, 0, 0, 0);
for ( $i = 0; $i < $n ; $i ++)
{
$temp = abs ( $arr [ $i ]);
if ( $temp <= $n )
$arr [ $temp - 1] = $arr [ $temp - 1] * (-1);
else if ( $temp > $n )
{
if ( $temp % $n != 0)
$helper [ $temp % $n - 1] = -1;
else
$helper [( $temp % $n ) + $n - 1] = -1;
}
}
for ( $i = 0; $i < $n ; $i ++)
if ( $arr [ $i ] > 0)
{
$a = $i + 1;
echo "$a" , " " ;
}
for ( $i = 0; $i < 4; $i ++)
if ( $helper [ $i ] >= 0)
{
$b = $n + $i + 1;
echo "$b" , " " ;
}
echo "\n" ;
return ;
}
$arr = array (1, 7, 3, 12, 5, 10, 8, 4, 9);
$n = sizeof( $arr );
missing4( $arr , $n );
?>
|
Javascript
<script>
function missing4(arr)
{
let helper =[];
for (let i = 0; i < 4; i++)
{
helper[i] = 0;
}
for (let i = 0; i < arr.length; i++)
{
let temp = Math.abs(arr[i]);
if (temp <= arr.length)
arr[temp - 1] = Math.floor(arr[temp - 1] * (-1));
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;
}
}
for (let i = 0; i < arr.length; i++)
if (arr[i] > 0)
document.write(i + 1 + " " );
for (let i = 0; i < helper.length; i++)
if (helper[i] >= 0)
document.write(arr.length + i + 1 + " " );
return ;
}
let arr = [ 1, 7, 3, 12, 5, 10, 8, 4, 9 ];
missing4(arr);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
28 Sep, 2022
Like Article
Save Article