From e524b6fdf042092c66b3b9e3e9faacc916a227be Mon Sep 17 00:00:00 2001 From: polarhub Date: Tue, 22 Aug 2017 23:09:13 -0600 Subject: [PATCH] equilibrium point function for booj coding test; resume@divyesh.tech. --- EquilibriumIndex.php | 53 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-) 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; + } } -