Hi! Could we please enable some services and cookies to improve your experience and our website?
Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code
<?php
class Solution {
/**
* @param Integer[] $nums
* @param Integer $target
* @return Integer
*/
static function search($nums, $target) {
$head = 0;
$tail = count($nums) - 1;
while($head <= $tail) {
$current = round(($head + $tail) / 2);
if ($nums[$current] == $target) {
return $current;
}
if ($nums[$current] < $target) {
$head = $current + 1;
} else {
$tail = $current - 1;
}
}
return -1;
}
}
var_dump(Solution::search([-1, 0, 3], 2));