Print missing elements that lie in range 0 – 99
Last Updated :
27 Jun, 2022
Given an array of integers print the missing elements that lie in range 0-99. If there are more than one missing, collate them, otherwise just print the number.
Note that the input array may not be sorted and may contain numbers outside the range [0-99], but only this range is to be considered for printing missing elements.
Examples :
Input: {88, 105, 3, 2, 200, 0, 10}
Output: 1
4-9
11-87
89-99
Input: {9, 6, 900, 850, 5, 90, 100, 99}
Output: 0-4
7-8
10-89
91-98
Expected time complexity O(n), where n is the size of the input array.
The idea is to use a boolean array of size 100 to keep track of array elements that lie in range 0 to 99. We first traverse input array and mark such present elements in the boolean array. Once all present elements are marked, the boolean array is used to print missing elements.
Following is the implementation of above idea.
C++
#include <bits/stdc++.h>
#define LIMIT 100
using namespace std;
void printMissing( int arr[], int n)
{
bool seen[LIMIT] = { false };
for ( int i=0; i<n; i++)
if (arr[i] < LIMIT)
seen[arr[i]] = true ;
int i = 0;
while (i < LIMIT)
{
if (seen[i] == false )
{
int j = i+1;
while (j < LIMIT && seen[j] == false )
j++;
(i+1 == j)? cout << i : cout << "\n" << i << "-" << j-1;
i = j;
}
else
i++;
}
}
int main()
{
int arr[] = {88, 105, 3, 2, 200, 0, 10};
int n = sizeof (arr)/ sizeof (arr[0]);
printMissing(arr, n);
return 0;
}
|
C
#include<stdio.h>
#define LIMIT 100
void printMissing( int arr[], int n)
{
bool seen[LIMIT] = { false };
for ( int i=0; i<n; i++)
if (arr[i] < LIMIT)
seen[arr[i]] = true ;
int i = 0;
while (i < LIMIT)
{
if (seen[i] == false )
{
int j = i+1;
while (j < LIMIT && seen[j] == false )
j++;
(i+1 == j)? printf ( "%dn" , i): printf ( "%d-%dn" , i, j-1);
i = j;
}
else
i++;
}
}
int main()
{
int arr[] = {88, 105, 3, 2, 200, 0, 10};
int n = sizeof (arr)/ sizeof (arr[0]);
printMissing(arr, n);
return 0;
}
|
Java
class PrintMissingElement
{
void printMissing( int arr[], int n)
{
int LIMIT = 100 ;
boolean seen[] = new boolean [LIMIT];
for ( int i = 0 ; i < LIMIT; i++)
seen[i] = false ;
for ( int i = 0 ; i < n; i++)
{
if (arr[i] < LIMIT)
seen[arr[i]] = true ;
}
int i = 0 ;
while (i < LIMIT)
{
if (seen[i] == false )
{
int j = i + 1 ;
while (j < LIMIT && seen[j] == false )
j++;
int p = j- 1 ;
System.out.println(i+ 1 ==j ? i : i + "-" + p);
i = j;
}
else
i++;
}
}
public static void main(String[] args)
{
PrintMissingElement missing = new PrintMissingElement();
int arr[] = { 88 , 105 , 3 , 2 , 200 , 0 , 10 };
int n = arr.length;
missing.printMissing(arr, n);
}
}
|
Python3
def printMissing(arr, n) :
LIMIT = 100
seen = [ False ] * LIMIT
for i in range (LIMIT) :
seen[i] = False
for i in range (n) :
if (arr[i] < LIMIT) :
seen[arr[i]] = True
i = 0
while (i < LIMIT) :
if (seen[i] = = False ) :
j = i + 1
while (j < LIMIT and seen[j] = = False ) :
j + = 1
p = j - 1
if (i + 1 = = j) :
print (i)
else :
print (i, "-" , p)
i = j
else :
i + = 1
arr = [ 88 , 105 , 3 , 2 , 200 , 0 , 10 ]
n = len (arr)
printMissing(arr, n)
|
C#
using System;
class GFG
{
static void printMissing( int [] arr, int n)
{
int LIMIT = 100;
bool [] seen = new bool [LIMIT];
int i;
for (i = 0; i < LIMIT; i++)
seen[i] = false ;
for (i = 0; i < n; i++)
{
if (arr[i] < LIMIT)
seen[arr[i]] = true ;
}
i = 0;
while (i < LIMIT)
{
if (seen[i] == false )
{
int j = i + 1;
while (j < LIMIT && seen[j] == false )
j++;
int p = j - 1;
if (i + 1 == j)
{
Console.WriteLine(i);
}
else
{
Console.WriteLine(i + "-" + p);
}
i = j;
}
else
i++;
}
}
static void Main()
{
int [] arr = {88, 105, 3, 2, 200, 0, 10};
int n = arr.Length;
printMissing(arr, n);
}
}
|
PHP
<?php
$LIMIT = 100;
function printMissing( $arr , $n )
{
global $LIMIT ;
$seen = (false);
for ( $i = 0; $i < $n ; $i ++)
if ( $arr [ $i ] < $LIMIT )
$seen [ $arr [ $i ]] = true;
$i = 0;
while ( $i < $LIMIT )
{
if ( $seen [ $i ] == false)
{
$j = $i + 1;
while ( $j < $LIMIT &&
$seen [ $j ] == false)
$j ++;
if (( $i + 1 == $j ) == true)
echo $i , "\n" ;
else
echo $i , "-" , $j - 1, "\n" ;
$i = $j ;
}
else
$i ++;
}
}
$arr = array (88, 105, 3, 2,
200, 0, 10);
$n = sizeof( $arr );
printMissing( $arr , $n );
?>
|
Javascript
<script>
function printMissing(arr, n)
{
let LIMIT = 100;
let seen = new Array(LIMIT);
let i;
for (i = 0; i < LIMIT; i++)
seen[i] = false ;
for (i = 0; i < n; i++)
{
if (arr[i] < LIMIT)
seen[arr[i]] = true ;
}
i = 0;
while (i < LIMIT)
{
if (seen[i] == false )
{
let j = i + 1;
while (j < LIMIT && seen[j] == false )
j++;
let p = j - 1;
if (i + 1 == j)
{
document.write(i + "</br>" );
}
else
{
document.write(i + "-" + p + "</br>" );
}
i = j;
}
else
i++;
}
}
let arr = [88, 105, 3, 2, 200, 0, 10];
let n = arr.length;
printMissing(arr, n);
</script>
|
Time complexity: O(n)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...