Skip to content

Instantly share code, notes, and snippets.

@alOneh
Created December 8, 2011 08:38
Show Gist options
  • Save alOneh/1446470 to your computer and use it in GitHub Desktop.
Save alOneh/1446470 to your computer and use it in GitHub Desktop.
<?php
function good_indexes($sequence) {
$size_of_sequence = count($sequence);
$sum_lower_indexes = 0;
for ($i = 0; $i <= $size_of_sequence; $i++)
{
if (!isset($sequence[$i]))
{
throw new Exception("Index : $i not exists in the sequence");
}
$sum_lower_indexes += $sequence[$i];
$sum_higher_indexes = 0;
for ($j = $i; $j < $size_of_sequence; $j++)
{
$sum_higher_indexes += $sequence[$j];
}
if ($sum_lower_indexes == $sum_higher_indexes) {
echo $i . "\n";
}
}
}
$sequence = array(-7, 1, 5, 2, -4, 3, 0);
try {
good_indexes($sequence);
} catch(Exception $e) {
echo $e->getMessage();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment