From 62f4af0d9f139267b7b41249cec2909647bbf983 Mon Sep 17 00:00:00 2001 From: krysler Date: Tue, 10 Jan 2017 19:56:11 -0430 Subject: [PATCH 1/3] Created feature branch --- EquilibriumIndex.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/EquilibriumIndex.php b/EquilibriumIndex.php index 020dd2b..2b521a4 100644 --- a/EquilibriumIndex.php +++ b/EquilibriumIndex.php @@ -4,6 +4,15 @@ function getEquilibriums($arr) { $output = array(); # Logic goes here! + if (is_array($arr)){ + $length = count($arr); + $left_acum = 0; + $right_acum = 0; + + for ($index=0; $index < $length; $index++) { + + } + } return $output; } From 361d67c775207109b8588fdc247e942530c0c08b Mon Sep 17 00:00:00 2001 From: krysler Date: Wed, 11 Jan 2017 20:55:19 -0430 Subject: [PATCH 2/3] Solution to equilibrium indexes and comments added --- EquilibriumIndex.php | 45 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/EquilibriumIndex.php b/EquilibriumIndex.php index 2b521a4..67a51ed 100644 --- a/EquilibriumIndex.php +++ b/EquilibriumIndex.php @@ -1,16 +1,53 @@ =0){ + $left_acum += $arr[$index-1]; + } + + // We start knowing the value of adding all the elements on the array ($arr), so if we start + // on the element on the left (index 0) and we substract its value from the sum of all array elements , + // we are going to get a value that represents the sum of all elements on the array minus the value of + // the element on index 0. If we store that last value and we keep substracting each array values, one by one,from left to right,on each iteration we get the sum of all elements to the right of the current index . + $right_acum -= $arr[$index]; + // If the sum of elements on the left is equal to the sum of elements to the right then this index is known + // as an equilibrium index and needs to be added to the return values. + if ($left_acum==$right_acum){ + // An array can have more than one equilibrium indexes, so add the equilibrium index to the + // array that is going to be returned + $output[]=$index; + } } } return $output; From 25c3b0fd2c0c425d99b84faf83fdf66a63d0829c Mon Sep 17 00:00:00 2001 From: krysler Date: Wed, 11 Jan 2017 21:29:23 -0430 Subject: [PATCH 3/3] Comments fixes --- EquilibriumIndex.php | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/EquilibriumIndex.php b/EquilibriumIndex.php index 67a51ed..233970e 100644 --- a/EquilibriumIndex.php +++ b/EquilibriumIndex.php @@ -19,33 +19,32 @@ function getEquilibriums($arr) { // Variable that is going to keep the sum of all array elements, from index 0 to index N-1 (N is array length) $left_acum = 0; - // Variable that holds the sum of all array elements + // Variable that holds the sum of all array elements. $right_acum = array_sum($arr); - // We need to iterate over all elements in the array $arr only once - // to get, on the same iteration, the sum of all elements on the right and sum of elements on the left of the - // current index + // We need to iterate over all elements in the array $arr only once. In order to get, on the same iteration, + // the sum of all elements on the right and sum of elements on the left of the current index. for ($index=0; $index < $length; $index++) { - // Add the value of the previous array element, if exists, to the variable that holds the - // sum of all array elements on that are on the left of the current index. - // Make sure the previous index is a valid index, this means bigger or equal than 0. + // Add the value of the previous array element, if exists, to the variable that holds the sum of all + // array elements on that are on the left of the current index. + // Make sure the previous index is a valid index, this means bigger or equal to 0. if ($index-1>=0){ $left_acum += $arr[$index-1]; } - // We start knowing the value of adding all the elements on the array ($arr), so if we start - // on the element on the left (index 0) and we substract its value from the sum of all array elements , - // we are going to get a value that represents the sum of all elements on the array minus the value of - // the element on index 0. If we store that last value and we keep substracting each array values, one by one,from left to right,on each iteration we get the sum of all elements to the right of the current index . + // We start knowing the value of adding all the elements on the array ($arr), so if we start on the + // element on the left (index 0) and we substract its value from the sum of all array elements , we + // are going to get a value that represents the sum of all elements on the array minus the value of the + // element on index 0. If we store that last value and we keep substracting each array value, one by one,from left to right,on each iteration we get the sum of all elements to the right of the current index. $right_acum -= $arr[$index]; // If the sum of elements on the left is equal to the sum of elements to the right then this index is known // as an equilibrium index and needs to be added to the return values. if ($left_acum==$right_acum){ // An array can have more than one equilibrium indexes, so add the equilibrium index to the - // array that is going to be returned + // array that is going to be returned. $output[]=$index; } }