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