# Find the missing elements from 1 to M in given N ranges | Set-2

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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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 ` `#define ll long long int ` `using` `namespace` `std; ` ` `  `// function to find the missing ` `// numbers from the given ranges ` `void` `findNumbers(vector > 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 > mergeRanges( ` `    ``vector > ranges, ``int` `m) ` `{ ` `    ``// sort all the ranges ` `    ``sort(ranges.begin(), ranges.end()); ` `    ``vector > ans; ` ` `  `    ``ll prevFirst = ranges.first, ` `       ``prevLast = ranges.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 > ranges; ` `    ``ranges.push_back({ 1, 2 }); ` `    ``ranges.push_back({ 4, 5 }); ` ` `  `    ``int` `n = ranges.size(); ` `    ``int` `m = 6; ` ` `  `    ``// this function returns merged ranges ` `    ``vector > mergedRanges ` `        ``= mergeRanges(ranges, n); ` ` `  `    ``// this function is use to find ` `    ``// missing numbers upto m ` `    ``findNumbers(mergedRanges, m); ` `    ``return` `0; ` `} `

Output:

```3 6
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.