Maximal Disjoint Intervals | Function to find maximal disjoint set ; Sort the list of intervals ; First Interval will always be included in set ; End point of first interval ; Check if given interval overlap with previously included interval , if not then include this interval and update the end point of last added interval ; Driver code
function maxDisjointIntervals ( list ) { list . sort ( ( a , b ) => a [ 1 ] - b [ 1 ] ) ; document . write ( " " + list [ 0 ] [ 0 ] + " " + list [ 0 ] [ 1 ] + " " + " " ) ; var r1 = list [ 0 ] [ 1 ] ; for ( var i = 1 ; i < list . length ; i ++ ) { var l1 = list [ i ] [ 0 ] ; var r2 = list [ i ] [ 1 ] ; if ( l1 > r1 ) { document . write ( " " + l1 + " " + r2 + " " + " " ) ; r1 = r2 ; } } } var N = 4 ; var intervals = [ [ 1 , 4 ] , [ 2 , 3 ] , [ 4 , 6 ] , [ 8 , 9 ] ] ; maxDisjointIntervals ( intervals ) ;