Hi... I got the following:
function solution($A) {
if (is_array($A)) {
if ($b = preg_grep("/\-1/", $A)) {
foreach ($b as $key => $value) {
$A_count = count($A);
$remove = ($A_count-1) - $key;
$v = $A[$key];
unset($A[$key]);
for($i = $key+1; $i < $A_count; $i++){
unset($A[$i]); // to remove the element after -1
}
$A[$key] = $v;
print_r($A);
$result = count($A);
}
}
}
return $result;
}
print_r(solution(array(1, 4, -1, 3, 2))); // returns 4
print_r(solution(array(2, -1, 1, 0, 0, 0))); // returns 3
print_r(solution(array(2, 1, 1, 0, 0, 0, -1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0))); // returns 8
I need the function to return 4 at all times. -1 is the last element of the array. please help?