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
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; } |
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
- Find four missing numbers in an array containing elements from 1 to N
- Find k-th smallest element in given n ranges
- Missing even and odd elements from the given arrays
- Find the kth element in the series generated by the given N ranges
- Print missing elements that lie in range 0 - 99
- Find the missing value from the given equation a + b = c
- Find the Missing Number
- Find the missing integer in an array if mean is given
- Find the smallest missing number
- Find n-variables from n sum equations with one missing
- Find the one missing number in range
- Find the missing number in Geometric Progression
- Find first k natural numbers missing in given 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.



