Skip to content

Instantly share code, notes, and snippets.

@georgo
Created January 2, 2017 09:50
Show Gist options
  • Select an option

  • Save georgo/6fa285df5e1c3a3d3ea5c271afea90d6 to your computer and use it in GitHub Desktop.

Select an option

Save georgo/6fa285df5e1c3a3d3ea5c271afea90d6 to your computer and use it in GitHub Desktop.
<?php
function equilib(&$arr) {
$left = array_sum($arr);
$right = 0;
foreach($arr as $index => $value) {
$left -= $value;
if ($left == $right) {
return $index;
}
$right += $value;
}
return -1;
}
$arr = [-7, 1, 5, 2, -4, 3, 0];
echo equilib($arr);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment