- array_slice: documentation ( source)
- round: documentation ( source)
- print_r: documentation ( source)
- max: documentation ( source)
- min: documentation ( source)
<?php
function solution($A) {
// write your code in PHP5
$result = -1;
$n = count($A);
if ($n > 0) {
if (0 && max($A) == $A[$n-1]) {
$result = $n-1;
} else {
$center = round($n/2);
while($center >1 && $center < $n){
$left = array_slice($A, 0, $center);
$right = array_slice($A, $center);
if (max($left) <= $A[$center]) {
if (min($right) >= $A[$center]) {
$result = $center;
break;
} else {
$center = round(($center + $n - 1)/2);
}
} else {
$center = round($center/2);
}
}
}
}
return $result;
}
$A[0] = 4 ;
$A[1] = 2 ;
$A[2] = 2;
$A[3] = 3 ;
$A[4] = 1 ;
$A[5] = 4;
$A[6] = 7 ;
$A[7] = 8 ;
$A[8] = 6;
$A[9] = 9;
print_r(solution($A));