Find the point where maximum intervals overlap | PHP Program to find maximum guest at any time in a party ; Sort arrival and exit arrays ; guests_in indicates number of guests at a time ; Similar to merge in merge sort to process all events in sorted order ; If next event in sorted order is arrival , increment count of guests ; Update max_guests if needed ; increment index of arrival array ; If event is exit , decrement count of guests . ; of guests . ; Driver Code
< ? php function findMaxGuests ( $ arrl , $ exit , $ n ) { sort ( $ arrl ) ; sort ( $ exit ) ; $ guests_in = 1 ; $ max_guests = 1 ; $ time = $ arrl [ 0 ] ; $ i = 1 ; $ j = 0 ; while ( $ i < $ n and $ j < $ n ) { if ( $ arrl [ $ i ] <= $ exit [ $ j ] ) { $ guests_in ++ ; if ( $ guests_in > $ max_guests ) { $ max_guests = $ guests_in ; $ time = $ arrl [ $ i ] ; } $ i ++ ; } else { $ guests_in -- ; $ j ++ ; } } echo " Maximum ▁ Number ▁ of ▁ Guests ▁ = ▁ " , $ max_guests , " ▁ at ▁ time ▁ " , $ time ; } $ arr1 = array ( 1 , 2 , 10 , 5 , 5 ) ; $ exit = array ( 4 , 5 , 12 , 9 , 120 ) ; $ n = count ( $ arr1 ) ; findMaxGuests ( $ arr1 , $ exit , $ n ) ; ? >