Created
December 8, 2011 08:38
-
-
Save alOneh/1446470 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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