Find the point where maximum intervals overlap |  ; Finding maximum starting time O ( n ) ; Finding maximum ending time O ( n ) ; Creating and auxiliary array O ( n ) ; Lazy addition ; Lazily Calculating value at index i O ( n ) ; Driver code
< ? php function maxOverlap ( $ start , $ end ) { $ n = count ( $ start ) ; $ maxa = max ( $ start ) ; $ maxb = max ( $ end ) ; $ maxc = max ( $ maxa , $ maxb ) ; $ x = array_fill ( 0 , $ maxc + 2 , 0 ) ; $ cur = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { ++ $ x [ $ start [ $ i ] ] ; -- $ x [ $ end [ $ i ] + 1 ] ; } $ maxy = - PHP_INT_MAX ; for ( $ i = 0 ; $ i <= $ maxc ; $ i ++ ) { $ cur += $ x [ $ i ] ; if ( $ maxy < $ cur ) { $ maxy = $ cur ; $ idx = $ i ; } } echo " Maximum ▁ value ▁ is ▁ " . $ maxy . " ▁ at ▁ position ▁ " . $ idx . " STRNEWLINE " ; } $ start = array ( 13 , 28 , 29 , 14 , 40 , 17 , 3 ) ; $ end = array ( 107 , 95 , 111 , 105 , 70 , 127 , 74 ) ; maxOverlap ( $ start , $ end ) ; ? >