3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function solution($A) { $n = sizeof($A); $result = 0; for ($i = 0; $i < $n - 1; $i++) { if ($A[$i] == $A[$i + 1]) $result = $result + 1; } $r = 0; for ($i = 0; $i < $n; $i++) { $count = 0; if ($i > 0) { if ($A[$i - 1] != $A[$i]) $count = $count + 1; else $count = $count - 1; } if ($i < $n - 1) { if ($A[$i + 1] != $A[$i]) $count = $count + 1; else $count = $count - 1; } $r = max($r, $count); } return $result + $r; } $A[0] = 1; $A[1] = 1; $A[2] = 0; $A[3] = 1 ; $A[4] = 0 ; $A[5] = 0; $A[6] = 0; print_r(solution($A));
Output for 4.3.0 - 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
5