Find the missing elements from 1 to M in given N ranges

Given N segments as ranges [L, R] where ranges are non-intersecting and non-overlapping. The task is to find all number between 1 to M that doesn’t belong to any of the given ranges.

Examples:

Input : N = 2, M = 6
        Ranges:
        [1, 2]
        [4, 5]
Output : 3, 6
Explanation: Only 3 and 6 are missing from
the above ranges.

Input : N = 1, M = 5
        Ranges:
        [2, 4]
Output : 1, 5

Approach: Given that we have N ranges, which are non-overlapping and non-intersecting. First of all, sort all segments based on starting value. After sorting, iterate from each segment and find the numbers which are missing.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find mssing elements
// from given Ranges
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to find mssing elements
// from given Ranges
void findMissingNumber(vector<pair<int, int> > ranges, int m)
{
    // First of all sort all the given ranges
    sort(ranges.begin(), ranges.end());
  
    // store ans in a different vector
    vector<int> ans;
  
    // prev is use to store end of
    // last range
    int prev = 0;
  
    // j is used as a counter for ranges
    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 segment
    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] << " ";
    }
}
  
// Driver code
int main()
{
    int N = 2, M = 6;
  
    // Store ranges in vector of pair
    vector<pair<int, int> > ranges;
    ranges.push_back({ 1, 2 });
    ranges.push_back({ 4, 5 });
  
    findMissingNumber(ranges, M);
  
    return 0;
}

chevron_right


“”

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find missing 
# elements from given Ranges 
  
# Function to find mssing elements 
# from given Ranges 
def findMissingNumber(ranges, m): 
  
    # First of all sort all the 
    # given ranges 
    ranges.sort() 
  
    # store ans in a different vector 
    ans = [] 
  
    # prev is use to store end
    # of last range
    prev = 0
  
    # j is used as a counter for ranges 
    for j in range(len(ranges)): 
        start = ranges[j][0
        end = ranges[j][1
  
        for i in range(prev + 1, start): 
            ans.append(i) 
  
        prev = end 
  
    # for last segment 
    for i in range(prev + 1, m + 1): 
        ans.append(i) 
  
    # finally print all answer 
    for i in range(len(ans)): 
        if ans[i] <= m:
            print(ans[i], end = " "
      
# Driver Code
if __name__ == "__main__":
      
    N, M = 2, 6
  
    # Store ranges in vector of pair 
    ranges = [] 
    ranges.append([1, 2]) 
    ranges.append([4, 5]) 
  
    findMissingNumber(ranges, M) 
  
# This code is contributed 
# by Rituraj Jain

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find missing
// elements from given Ranges
  
// Function to find mssing elements
// from given Ranges
function findMissingNumber($ranges, $m)
{
    // First of all sort all the
    // given ranges
    sort($ranges);
      
    // store ans in a different vector
    $ans = [];
      
    // prev is use to store end
    // of last range
    $prev = 0;
      
    // j is used as a counter for ranges
    for ($j = 0; $j < count($ranges); $j++) 
    {
        $start = $ranges[$j][0];
        $end = $ranges[$j][1];
      
        for ($i = $prev + 1; $i < $start; $i++) 
            array_push($ans, $i);
              
        $prev = $end;
    }
      
    // for last segment
    for ($i = $prev + 1; $i < $m + 1; $i++)
            array_push($ans, $i);
      
    // finally print all answer
    for ($i = 0; $i < count($ans); $i++) 
    {
        if ($ans[$i] <= $m)
            echo "$ans[$i] ";
    }
}
  
// Driver Code
$N = 2;
$M = 6;
  
// Store ranges in vector of pair
$ranges = [];
array_push($ranges, [1, 2]);
array_push($ranges, [4, 5]);
  
findMissingNumber($ranges, $M)
  
// This code is contributed
// by Srathore
?>

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.