Find the only missing number in a sorted array | PHP program to find the only missing element . ; If this is the first element which is not index + 1 , then missing element is mid + 1 ; if this is not the first missing element search in left side ; if it follows index + 1 property then search in right side ; if no element is missing ; Driver Code
< ? php function findmissing ( & $ ar , $ N ) { $ r = $ N - 1 ; $ l = 0 ; while ( $ l <= $ r ) { $ mid = ( $ l + $ r ) / 2 ; if ( $ ar [ $ mid ] != $ mid + 1 && $ ar [ $ mid - 1 ] == $ mid ) return ( $ mid + 1 ) ; if ( $ ar [ $ mid ] != $ mid + 1 ) $ r = $ mid - 1 ; else $ l = $ mid + 1 ; } return ( -1 ) ; } $ ar = array ( 1 , 2 , 3 , 4 , 5 , 7 , 8 ) ; $ N = sizeof ( $ ar ) ; echo ( findmissing ( $ ar , $ N ) ) ; ? >