Given an integer and ranges (e.g. [a, b]) which are intersecting and overlapping. The task is to find all the number within the range that doesn’t belong to any of the given ranges.

**Examples:**

Input:m = 6, ranges = {{1, 2}, {4, 5}}

Output:3 6

As only 3 and 6 are missing from the given ranges.

Input:m = 5, ranges = {{2, 4}}

Output:1 5

**Approach:** As we have ranges, if ranges are non-overlapping and non-intersecting then follow the approach described here.

But here are overlapping and intersecting ranges, so first merge all the ranges so that there are no overlapping or intersecting ranges.

After merging is done, iterate from each range and find the numbers which are missing.

Below is the implementation of the above approach:

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `#define ll long long int ` `using` `namespace` `std; ` ` ` `// function to find the missing ` `// numbers from the given ranges ` `void` `findNumbers(vector<pair<` `int` `, ` `int` `> > ranges, ` `int` `m) ` `{ ` ` ` `vector<` `int` `> ans; ` ` ` ` ` `// prev is use to store end of last range ` ` ` `int` `prev = 0; ` ` ` ` ` `// j is used as counter for range ` ` ` `for` `(` `int` `j = 0; j < ranges.size(); j++) { ` ` ` `int` `start = ranges[j].first; ` ` ` `int` `end = ranges[j].second; ` ` ` `for` `(` `int` `i = prev + 1; i < start; i++) ` ` ` `ans.push_back(i); ` ` ` `prev = end; ` ` ` `} ` ` ` ` ` `// for last range ` ` ` `for` `(` `int` `i = prev + 1; i <= m; i++) ` ` ` `ans.push_back(i); ` ` ` ` ` `// finally print all answer ` ` ` `for` `(` `int` `i = 0; i < ans.size(); i++) ` ` ` `if` `(ans[i] <= m) ` ` ` `cout << ans[i] << ` `" "` `; ` `} ` ` ` `// function to return the ranges after merging ` `vector<pair<` `int` `, ` `int` `> > mergeRanges( ` ` ` `vector<pair<` `int` `, ` `int` `> > ranges, ` `int` `m) ` `{ ` ` ` `// sort all the ranges ` ` ` `sort(ranges.begin(), ranges.end()); ` ` ` `vector<pair<` `int` `, ` `int` `> > ans; ` ` ` ` ` `ll prevFirst = ranges[0].first, ` ` ` `prevLast = ranges[0].second; ` ` ` ` ` `// merging of overlapping ranges ` ` ` `for` `(` `int` `i = 0; i < m; i++) { ` ` ` `ll start = ranges[i].first; ` ` ` `ll last = ranges[i].second; ` ` ` ` ` `// ranges do not overlap ` ` ` `if` `(start > prevLast) { ` ` ` `ans.push_back({ prevFirst, prevLast }); ` ` ` `prevFirst = ranges[i].first; ` ` ` `prevLast = ranges[i].second; ` ` ` `} ` ` ` `else` ` ` `prevLast = last; ` ` ` ` ` `if` `(i == m - 1) ` ` ` `ans.push_back({ prevFirst, prevLast }); ` ` ` `} ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `// vector of pair to store the ranges ` ` ` `vector<pair<` `int` `, ` `int` `> > ranges; ` ` ` `ranges.push_back({ 1, 2 }); ` ` ` `ranges.push_back({ 4, 5 }); ` ` ` ` ` `int` `n = ranges.size(); ` ` ` `int` `m = 6; ` ` ` ` ` `// this function returns merged ranges ` ` ` `vector<pair<` `int` `, ` `int` `> > mergedRanges ` ` ` `= mergeRanges(ranges, n); ` ` ` ` ` `// this function is use to find ` ` ` `// missing numbers upto m ` ` ` `findNumbers(mergedRanges, m); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

3 6

## Recommended Posts:

- Find the missing elements from 1 to M in given N ranges
- Find a range that covers all the elements of given N ranges
- Count distinct median possible for an Array using given ranges of elements
- Find k-th smallest element in given n ranges
- Find the kth element in the series generated by the given N ranges
- Minimum change in given value so that it lies in all given Ranges
- Find four missing numbers in an array containing elements from 1 to N
- Find missing elements from an Array
- Missing even and odd elements from the given arrays
- Find if there exists a direction for ranges such that no two range intersect
- Find all ranges of consecutive numbers from Array
- GCDs of given index ranges in an array
- Queries on probability of even or odd number in given ranges
- Search an element in given N ranges
- Maximum pair sum in the given index ranges of an Array
- Queries for maximum and minimum difference between Fibonacci numbers in given ranges
- Find first k natural numbers missing in given array
- Find the missing integer in an array if mean is given
- Find the missing value from the given equation a + b = c
- Find all missing numbers from a given sorted array

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.