Repeatedly search an element by doubling it after every successful search | Php program to repeatedly search an element by doubling it after every successful search ; Sort the given array so that binary search can be applied on it ; Maximum array element ; search for the element b present or not in array ; Driver code
< ? php function binary_search ( $ a , $ x , $ lo = 0 , $ hi = NULL ) { if ( $ hi == NULL ) $ hi = count ( $ a ) ; while ( $ lo < $ hi ) { $ mid = ( $ lo + $ hi ) / 2 ; $ midval = $ a [ $ mid ] ; if ( $ midval < $ x ) $ lo = $ mid + 1 ; else if ( $ midval > $ x ) $ hi = $ mid ; else return $ mid ; } return -1 ; } function findElement ( $ a , $ n , $ b ) { sort ( $ a ) ; $ mx = $ a [ $ n - 1 ] ; while ( $ b < max ( $ a ) ) { if ( binary_search ( $ a , $ b , 0 , $ n ) != -1 ) $ b *= 2 ; else return $ b ; } return $ b ; } $ a = array ( 1 , 2 , 3 ) ; $ n = count ( $ a ) ; $ b = 1 ; echo findElement ( $ a , $ n , $ b ) ; ? >