Title: Determine Equal Sides Of An Array
Question:
I need help writing a JS function that sums the left and right integers on either side of an incrementing index until it returns the index and integer where the left and right integer sums are equivalent.
Abstract Example:
Note: "|x|" is the indexed integer that separates the left and right integer sums.
//ignore "|x|" when adding each side, and Sum both "Left|x|Right" sides until the Right and Left side are determined "===" (eg.equivalent)
SUM LEFT <|x|> RIGHT SUM CHECK
1 = [|1|2,3,4,3,2,1 ] = 15 //!==
1 = [ 1|2|3,4,3,2,1 ] = 13 //!==
3 = [ 1,2|3|4,3,2,1 ] = 10 //!==
6 = [ 1,2,3|4|3,2,1 ] = 6 //===
10 = [ 1,2,3,4|3|2,1 ] = 3 //!==
13 = [ 1,2,3,4,3|2|1 ] = 1 //!==
15 = [ 1,2,3,4,3,2|1|] = 1 //!==
Answer:
In this example, index 3 on integer 4 splits the array where both the left and right side sums are equivalent.
Return:
When the function determines that both the left and right sides are equivalent, it should return the indexed integer (eg. 4). If neither side is equivalent during all iterations, then it should return -1.
Thank you!
The solution you have come up with works, however it could be very inefficient with larger arrays.
Instead, consider an approach where you keep a running total so that you're not constantly re-adding entire sections of the array.
Here is a timed comparison of your solution and mine:
Even with a very small array of just 7 items, my solution is an order of magnitude faster, and with the larger array mine is 400 times faster!