Count the number of intervals in which a given value lies | PHP program to count the number of intervals in which a given value lies ; Function to count the number of intervals in which a given value lies ; Variables to store overall minimum and maximum of the intervals ; Variables to store start and end of an interval ; Frequency array to keep track of how many of the given intervals an element lies in ; Constructing the frequency array ; Driver code ; length of the array
< ? php $ MAX_VAL = 200000 ; function countIntervals ( $ arr , $ V , $ N ) { global $ MAX_VAL ; $ min = PHP_INT_MAX ; $ max = 0 ; $ li = 0 ; $ ri = 0 ; $ freq = array_fill ( 0 , $ MAX_VAL , 0 ) ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { $ li = $ arr [ $ i ] [ 0 ] ; $ freq [ $ li ] = $ freq [ $ li ] + 1 ; $ ri = $ arr [ $ i ] [ 1 ] ; $ freq [ $ ri + 1 ] = $ freq [ $ ri + 1 ] - 1 ; if ( $ li < $ min ) $ min = $ li ; if ( $ ri > $ max ) $ max = $ ri ; } for ( $ i = $ min ; $ i <= $ max ; $ i ++ ) $ freq [ $ i ] = $ freq [ $ i ] + $ freq [ $ i - 1 ] ; return $ freq [ $ V ] ; } $ arr = array ( array ( 1 , 10 ) , array ( 5 , 10 ) , array ( 15 , 25 ) , array ( 7 , 12 ) , array ( 20 , 25 ) ) ; $ V = 7 ; $ N = count ( $ arr ) ; echo ( countIntervals ( $ arr , $ V , $ N ) ) ; ? >