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++
// C++ program for print missing elements #include <bits/stdc++.h> #define LIMIT 100 using namespace std; // A O(n) function to print missing elements in an array 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)? cout << i : cout << "\n" << i << "-" << j-1; // Update u i = j; } else i++; } } // Driver program int main() { int arr[] = {88, 105, 3, 2, 200, 0, 10}; int n = sizeof(arr)/sizeof(arr[0]); printMissing(arr, n); return 0; } // This code is contributed by shivanisinghss2110 |
C
// C program for print missing elements #include<stdio.h> #define LIMIT 100 // A O(n) function to print missing elements in an array 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("%dn", i): printf("%d-%dn", i, j-1); // Update u i = j; } else i++; } } // Driver program 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 { // A O(n) function to print missing elements in an array void printMissing(int arr[], int n) { int LIMIT = 100; boolean seen[] = new boolean[LIMIT]; // Initialize all number from 0 to 99 as NOT seen for (int i = 0; i < LIMIT; i++) seen[i] = 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 int p = j-1; System.out.println(i+1==j ? i : i + "-" + p); // Update u i = j; } else i++; } } // Driver program to test above functions 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); } } |
PHP
<?php // PHP program for print // missing elements $LIMIT= 100; // A O(n) function to print // missing elements in an array function printMissing($arr, $n) { global $LIMIT; // Initialize all number from // 0 to 99 as NOT seen $seen = (false); // Mark present elements in. // range [0-99] as seen for ($i = 0; $i < $n; $i++) if ($arr[$i] < $LIMIT) $seen[$arr[$i]] = true; // Print missing element $i = 0; while ($i < $LIMIT) { // If i is missing if ($seen[$i] == false) { // Find if there are more // missing elements after i $j = $i + 1; while ($j < $LIMIT && $seen[$j] == false) $j++; // Print missing // single or range if (($i + 1 == $j) == true) echo $i, "\n"; else echo $i , "-", $j - 1, "\n"; // Update u $i = $j; } else $i++; } } // Driver Code $arr = array (88, 105, 3, 2, 200, 0, 10); $n = sizeof($arr); printMissing($arr, $n); // This code is contributed by aj_36 ?> |
Output :
1 4-9 11-87 89-99
Time complexity of the above program is O(n).
This article is contributed by Vignesh Narayanan and Sowmya Sampath. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Find missing elements of a range
- Find the one missing number in range
- Find the missing number in a sorted array of limited range
- Missing even and odd elements from the given arrays
- Find missing elements from an Array
- Find the missing elements from 1 to M in given N ranges | Set-2
- Queries for elements having values within the range A to B in the given index range using Segment Tree
- Range Queries to count elements lying in a given Range : MO's Algorithm
- Find four missing numbers in an array containing elements from 1 to N
- Missing occurrences of a number in an array such that maximum absolute difference of adjacent elements is minimum
- Elements to be added so that all elements of a range are present in array
- Sum of distinct elements when elements are in range 1 to n
- Sum of elements of an AP in the given range
- XOR of a subarray (range of elements) | Set 2
- XOR of a subarray (range of elements)
- Sum of multiples of Array elements within a given range [L, R]
- Find the XOR of the elements in the given range [L, R] with the value K for a given set of queries
- Sum of elements of a Geometric Progression (GP) in a given range
- Construct sum-array with sum of elements in given range
- Check if an array contains all elements of a given range

