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
< ? php function findMissingNumber ( $ ranges , $ m ) { sort ( $ ranges ) ; $ ans = [ ] ; $ prev = 0 ; 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 ( $ i = $ prev + 1 ; $ i < $ m + 1 ; $ i ++ ) array_push ( $ ans , $ i ) ; for ( $ i = 0 ; $ i < count ( $ ans ) ; $ i ++ ) { if ( $ ans [ $ i ] <= $ m ) echo " $ ans [ $ i ] ▁ " ; } } $ N = 2 ; $ M = 6 ; $ ranges = [ ] ; array_push ( $ ranges , [ 1 , 2 ] ) ; array_push ( $ ranges , [ 4 , 5 ] ) ; findMissingNumber ( $ ranges , $ M ) ? >