Find Intersection of all Intervals | Function to print the intersection ; First interval ; Check rest of the intervals and find the intersection ; If no intersection exists ; Else update the intersection ; Driver code
< ? php function findIntersection ( $ intervals , $ N ) { $ l = $ intervals [ 0 ] [ 0 ] ; $ r = $ intervals [ 0 ] [ 1 ] ; for ( $ i = 1 ; $ i < $ N ; $ i ++ ) { if ( $ intervals [ $ i ] [ 0 ] > $ r $ intervals [ $ i ] [ 1 ] < $ l ) { echo - 1 ; return ; } else { $ l = max ( $ l , $ intervals [ $ i ] [ 0 ] ) ; $ r = min ( $ r , $ intervals [ $ i ] [ 1 ] ) ; } } echo " [ " ▁ . ▁ $ l ▁ . ▁ " , " ▁ . ▁ $ r ▁ . ▁ " ] " ; } $ intervals = array ( array ( 1 , 6 ) , array ( 2 , 8 ) , array ( 3 , 10 ) , array ( 5 , 8 ) ) ; $ N = sizeof ( $ intervals ) ; findIntersection ( $ intervals , $ N ) ; ? >