Find the missing elements from 1 to M in given N 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
def findMissingNumber ( ranges , m ) : NEW_LINE INDENT ranges . sort ( ) NEW_LINE ans = [ ] NEW_LINE prev = 0 NEW_LINE for j in range ( len ( ranges ) ) : NEW_LINE INDENT start = ranges [ j ] [ 0 ] NEW_LINE end = ranges [ j ] [ 1 ] NEW_LINE for i in range ( prev + 1 , start ) : NEW_LINE INDENT ans . append ( i ) NEW_LINE DEDENT prev = end NEW_LINE DEDENT for i in range ( prev + 1 , m + 1 ) : NEW_LINE INDENT ans . append ( i ) NEW_LINE DEDENT for i in range ( len ( ans ) ) : NEW_LINE INDENT if ans [ i ] <= m : NEW_LINE INDENT print ( ans [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N , M = 2 , 6 NEW_LINE ranges = [ ] NEW_LINE ranges . append ( [ 1 , 2 ] ) NEW_LINE ranges . append ( [ 4 , 5 ] ) NEW_LINE findMissingNumber ( ranges , M ) NEW_LINE DEDENT