Find the Missing Number in a sorted array | A binary search based program to find the only missing number in a sorted array of distinct elements within limited range . ; Driver Code
< ? php function search ( $ ar , $ size ) { $ a = 0 ; $ b = $ size - 1 ; $ mid ; while ( ( $ b - $ a ) > 1 ) { $ mid = ( int ) ( ( $ a + $ b ) / 2 ) ; if ( ( $ ar [ $ a ] - $ a ) != ( $ ar [ $ mid ] - $ mid ) ) $ b = $ mid ; else if ( ( $ ar [ $ b ] - $ b ) != ( $ ar [ $ mid ] - $ mid ) ) $ a = $ mid ; } return ( $ ar [ $ a ] + 1 ) ; } $ ar = array ( 1 , 2 , 3 , 4 , 5 , 6 , 8 ) ; $ size = sizeof ( $ ar ) ; echo " Missing ▁ number : ▁ " , search ( $ ar , $ size ) ; ? >