Check if any two intervals intersects among a given set of intervals | An interval has start time and end time ; Function to check if any two intervals overlap ; Find the overall maximum element ; Initialize an array of size max_ele ; starting point of the interval ; end point of the interval ; Calculating the prefix Sum ; Overlap ; If we reach here , then no Overlap ; Driver program
class Interval { constructor ( start , end ) { this . start = start ; this . end = end ; } } function isIntersect ( arr , n ) { let max_ele = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( max_ele < arr [ i ] . end ) max_ele = arr [ i ] . end ; } let aux = new Array ( max_ele + 1 ) ; for ( let i = 0 ; i < ( max_ele + 1 ) ; i ++ ) { aux [ i ] = 0 ; } for ( let i = 0 ; i < n ; i ++ ) { let x = arr [ i ] . start ; let y = arr [ i ] . end ; aux [ x ] ++ ; aux [ y ] -- ; } for ( let i = 1 ; i <= max_ele ; i ++ ) { aux [ i ] += aux [ i - 1 ] ; if ( aux [ i ] > 1 ) return true ; } return false ; } let arr1 = [ new Interval ( 1 , 3 ) , new Interval ( 7 , 9 ) , new Interval ( 4 , 6 ) , new Interval ( 10 , 13 ) ] ; let n1 = arr1 . length ; if ( isIntersect ( arr1 , n1 ) ) document . write ( " " ) ; else document . write ( " " ) ; let arr2 = [ new Interval ( 6 , 8 ) , new Interval ( 1 , 3 ) , new Interval ( 2 , 4 ) , new Interval ( 4 , 7 ) ] ; let n2 = arr2 . length ; if ( isIntersect ( arr2 , n2 ) ) document . write ( " " ) ; else document . write ( " " ) ;