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 arrayInput: 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[].
// C++ program to calculate missing ranges in an array
#include <iostream>
#include <vector>
using namespace std;
vector<vector<int>> missingRanges(vector<int> &arr, int lower, int upper) {
int n = arr.size();
vector<vector<int>> res;
// Check for missing range before the first element
if (lower < arr[0])
res.push_back({lower, arr[0] - 1});
// Check for missing ranges between consecutive elements
for (int i = 0; i < n - 1; ++i)
if (arr[i + 1] - arr[i] > 1)
res.push_back({arr[i] + 1, arr[i + 1] - 1});
// Check for missing range after the last element
if (n > 0 && upper > arr[n - 1])
res.push_back({arr[n - 1] + 1, upper});
return res;
}
int main() {
int lower = 10, upper = 50;
vector<int> arr{14, 15, 20, 30, 31, 45};
vector<vector<int>> res = missingRanges(arr, lower, upper);
for (const vector<int> &v : res)
cout << v[0] << " " << v[1] << endl;
return 0;
}
// Java program to calculate missing ranges in an array
import java.util.*;
class GfG {
public static List<List<Integer>> missingRanges(int[] arr, int lower, int upper) {
int n = arr.length;
List<List<Integer>> res = new ArrayList<>();
// Check for missing range before the first element
if (lower < arr[0]) {
res.add(Arrays.asList(lower, arr[0] - 1));
}
// Check for missing ranges between consecutive elements
for (int i = 0; i < n - 1; ++i) {
if (arr[i + 1] - arr[i] > 1) {
res.add(Arrays.asList(arr[i] + 1, arr[i + 1] - 1));
}
}
// Check for missing range after the last element
if (upper > arr[n - 1]) {
res.add(Arrays.asList(arr[n - 1] + 1, upper));
}
return res;
}
public static void main(String[] args) {
int lower = 10;
int upper = 50;
int[] arr = {14, 15, 20, 30, 31, 45};
List<List<Integer>> res = missingRanges(arr, lower, upper);
for (List<Integer> range : res) {
System.out.println(range.get(0) + " " + range.get(1));
}
}
}
# Python program to calculate missing ranges in an array
def missingRanges(arr, lower, upper):
n = len(arr)
res = []
# Check for missing range before the first element
if lower < arr[0]:
res.append([lower, arr[0] - 1])
# Check for missing ranges between consecutive elements
for i in range(n - 1):
if arr[i + 1] - arr[i] > 1:
res.append([arr[i] + 1, arr[i + 1] - 1])
# Check for missing range after the last element
if upper > arr[-1]:
res.append([arr[-1] + 1, upper])
return res
if __name__ == "__main__":
lower = 10
upper = 50
arr = [14, 15, 20, 30, 31, 45]
res = missingRanges(arr, lower, upper)
for v in res:
print(v[0], v[1])
// C# program to calculate missing ranges in an array
using System;
using System.Collections.Generic;
public class GfG
{
public static List<List<int>> MissingRanges(int[] arr, int lower, int upper) {
int n = arr.Length;
List<List<int>> res = new List<List<int>>();
// Check for missing range before the first element
if (lower < arr[0]) {
res.Add(new List<int> { lower, arr[0] - 1 });
}
// Check for missing ranges between consecutive elements
for (int i = 0; i < n - 1; i++) {
if (arr[i + 1] - arr[i] > 1) {
res.Add(new List<int> { arr[i] + 1, arr[i + 1] - 1 });
}
}
// Check for missing range after the last element
if (upper > arr[n - 1]) {
res.Add(new List<int> { arr[n - 1] + 1, upper });
}
return res;
}
public static void Main(string[] args) {
int lower = 10;
int upper = 50;
int[] arr = { 14, 15, 20, 30, 31, 45 };
List<List<int>> res = MissingRanges(arr, lower, upper);
foreach (var range in res) {
Console.WriteLine($"{range[0]} {range[1]}");
}
}
}
// JavaScript program to calculate missing ranges in an array
function missingRanges(arr, lower, upper) {
const n = arr.length;
const res = [];
// Check for missing range before the first element
if (lower < arr[0]) {
res.push([lower, arr[0] - 1]);
}
// Check for missing ranges between consecutive elements
for (let i = 0; i < n - 1; i++) {
if (arr[i + 1] - arr[i] > 1) {
res.push([arr[i] + 1, arr[i + 1] - 1]);
}
}
// Check for missing range after the last element
if (upper > arr[n - 1]) {
res.push([arr[n - 1] + 1, upper]);
}
return res;
}
// Driver code
const lower = 10;
const upper = 50;
const arr = [14, 15, 20, 30, 31, 45];
const res = missingRanges(arr, lower, upper);
for (const range of res) {
console.log(`${range[0]} ${range[1]}`);
}
Output
10 13 16 19 21 29 32 44 46 50