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

Given an integer m and n ranges (e.g. [a, b]) which are intersecting and overlapping. The task is to find all the number within the range [1, m] 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 n 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


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.