![]() |
VOOZH | about |
Given an array, arr[0..n-1] of distinct elements and a range [low, high], find all numbers that are in a range, but not the array. The missing elements should be printed in sorted order.
Examples:
Input: arr[] = {10, 12, 11, 15},
low = 10, high = 15
Output: 13, 14
Input: arr[] = {1, 14, 11, 51, 15},
low = 50, high = 55
Output: 50, 52, 53, 54 55
The naive approach for the problem can be to use two nested loops: one to traverse numbers from low to high and other one to traverse entire array to find out whether the element of the outer loop exists in the array or not. If it doesn't exist we will print it else continue to next iteration.
2 6 7 8 9 10
Sort the array, then do a binary search for 'low'. Once the location of low is found, start traversing the array from that location and keep printing all missing numbers.
2 6 7 8 9 10
Create a boolean array, where each index will represent whether the (i+low)th element is present in the array or not. Mark all those elements which are in the given range and are present in the array. Once all array items present in the given range have been marked true in the array, we traverse through the Boolean array and print all elements whose value is false.
2 6 7 8 9 10
Create a hash table and insert all array items into the hash table. Once all items are in hash table, traverse through the range and print all missing elements.
2 6 7 8 9 10
Which approach is better?
The time complexity of the first approach is O(nLogn + k) where k is the number of missing elements (Note that k may be more than n Log n if the array is small and the range is big)
The time complexity of the second and third solutions is O(n + (high-low+1)).
If the given array has almost all elements of the range, i.e., n is close to the value of (high-low+1), then the second and third approaches are definitely better as there is no Log n factor. But if n is much smaller than the range, then the first approach is better as it doesn't require extra space for hashing. We can also modify the first approach to print adjacent missing elements as range to save time. For example, if 50, 51, 52, 53, 54, 59 are missing, we can print them as 50-54, 59 in the first method. And if printing this way is allowed, the first approach takes only O(n Log n) time. Out of the Second and Third Solutions, the second solution is better because the worst-case time complexity of the second solution is better than the third.