diff --git a/EquilibriumIndex.php b/EquilibriumIndex.php index 020dd2b..e82510d 100644 --- a/EquilibriumIndex.php +++ b/EquilibriumIndex.php @@ -1,9 +1,58 @@ right total, + // add the next available element on the right to the right total + // and move the right index towards the equlibrium point if any + if ($lTot > $rTot) { // redundant if stmt - for readability, remove for performance + $rIdx--; + $rTot += $arr[$rIdx]; + } + } + } + + // we've come to a point where there's only one element bet our two converging indexes + // verify that and see if the two sides have the same total + if (($lIdx == ($rIdx - 2)) && ($rTot == $lTot)) { + $output[0] = $lIdx + 1; // if yes, return the element between the two indexes + $output[1] = $ecount - 1; + } else { // return an empty array + return $output; + } } -