Print missing elements that lie in range 0 - 99 - GeeksforGeeks
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.
Read full article from Print missing elements that lie in range 0 - 99 - GeeksforGeeks
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.
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.
void
printMissing(
int
arr[],
int
n)
{
// Initialize all number from 0 to 99 as NOT seen
bool
seen[LIMIT] = {
false
};
// Mark present elements in range [0-99] as seen
for
(
int
i=0; i<n; i++)
if
(arr[i] < LIMIT)
seen[arr[i]] =
true
;
// Print missing element
int
i = 0;
while
(i < LIMIT)
{
// If i is missing
if
(seen[i] ==
false
)
{
// Find if there are more missing elements after i
int
j = i+1;
while
(j < LIMIT && seen[j] ==
false
)
j++;
// Print missing single or range
(i+1 == j)?
printf
(
"%d\n"
, i):
printf
(
"%d-%d\n"
, i, j-1);
// Update u
i = j;
}
else
i++;
}
}