Find the point where maximum intervals overlap | Javascript implementation of above approach ; Finding maximum starting time ; Finding maximum ending time ; reating an auxiliary array ; Lazy addition ; Lazily Calculating value at index i ; Driver code
function maxOverlap ( start , end , n ) { let maxa = 0 ; for ( let i = 0 ; i < start . length ; i ++ ) { maxa = Math . max ( maxa , start [ i ] ) ; } let maxb = 0 ; for ( let i = 0 ; i < end . length ; i ++ ) { maxb = Math . max ( maxb , end [ i ] ) ; } let maxc = Math . max ( maxa , maxb ) ; let x = new Array ( maxc + 2 ) ; x . fill ( 0 ) ; let cur = 0 , idx = 0 ; for ( let i = 0 ; i < n ; i ++ ) { ++ x [ start [ i ] ] ; -- x [ end [ i ] + 1 ] ; } let maxy = Number . MIN_VALUE ; for ( let i = 0 ; i <= maxc ; i ++ ) { cur += x [ i ] ; if ( maxy < cur ) { maxy = cur ; idx = i ; } } document . write ( " " + maxy + " " + idx + " " ) ; } let start = [ 13 , 28 , 29 , 14 , 40 , 17 , 3 ] ; let end = [ 107 , 95 , 111 , 105 , 70 , 127 , 74 ] ; let n = start . length ; maxOverlap ( start , end , n ) ;