Count number of elements between two given elements in array | Function to count number of elements occurs between the elements . ; Find num1 ; If num1 is not present or present at end ; Find num2 ; If num2 is not present ; return number of elements betweenthe two elements . ; Driver Code
< ? php function getCount ( $ arr , $ n , $ num1 , $ num2 ) { $ i = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ arr [ $ i ] == $ num1 ) break ; if ( $ i >= $ n - 1 ) return 0 ; $ j ; for ( $ j = $ n - 1 ; $ j >= $ i + 1 ; $ j -- ) if ( $ arr [ $ j ] == $ num2 ) break ; if ( $ j == $ i ) return 0 ; return ( $ j - $ i - 1 ) ; } $ arr = array ( 3 , 5 , 7 , 6 , 4 , 9 , 12 , 4 , 8 ) ; $ n = sizeof ( $ arr ) ; $ num1 = 5 ; $ num2 = 4 ; echo ( getCount ( $ arr , $ n , $ num1 , $ num2 ) ) ; ? >