VOOZH about

URL: https://www.geeksforgeeks.org/dsa/missing-ranges-of-numbers/

⇱ Missing ranges of numbers - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Missing ranges of numbers

Last Updated : 2 Jan, 2025

You have an inclusive interval [lower, upper] and a sorted array of unique integers arr[], all of which lie within this interval. A number x is considered missing if x is in the range [lower, upper] but not present in arr. Your task is to return the smallest set of sorted ranges that includes all missing numbers, ensuring no element from arr is within any range, and every missing number is covered exactly once.

Examples

Input: arr[] = [14, 15, 20, 30, 31, 45], lower = 10, upper = 50
Output: [[10, 13], [16, 19], [21, 29], [32, 44], [46, 50]]
Explanation: These ranges represent all missing numbers between 10 and 50 not present in the array

Input: arr[] = [-48, -10, -6, -4, 0, 4, 17], lower = -54, upper = 17
Output: [[-54, -49], [-47, -11], [-9, -7], [-5, -5], [-3, -1], [1, 3], [5,16]]
Explanation: These ranges represent all missing numbers between -54 and 17 not present in the array.

Using Linear Scan - O(n) Time and O(1) Space

Since arr[] is sorted, we can efficiently identify gaps between consecutive elements. By iterating through arr[], we check the difference between adjacent numbers:

  • If the difference is greater than one, the numbers in between form a missing range.
  • Additionally, we handle edge cases where the missing range starts before the first element or ends after the last element of arr[].

Output
10 13
16 19
21 29
32 44
46 50


Comment
Article Tags:
Article Tags: