
Solution in php:
function solution($A) {
$result = 0;
$cnt = count($A);
$dps = array_fill(0, $cnt, 0);
$dpe = array_fill(0, $cnt, 0);
for ($i = 0; $i < $cnt; $i++){
$dps[max(0, $i - $A[$i])]++;
$dpe[min($cnt - 1, $i + $A[$i])]++;
}
$t = 0;
for ($i = 0; $i 0){
$result += $t * $dps[$i];
$result += $dps[$i] * ($dps[$i] - 1) / 2;
if($result > 10000000){
return -1;
}
$t += $dps[$i];
}
$t -= $dpe[$i];
}
return $result;
}
Given “AS IS”, can be ported from other languages from solutions found on the internet, please use with care.
Please note: we think that codility.com does not give a correct assessment of your real-world programming skills. For instance, reading the below would bring more understanding why: http://codility-test-questions.blogspot.com/2013/01/my-experience-with-codility-test.html

Clever solution, helped me with mine but this way seems to make more sense 😀