← Back to challenges

The Array Twins

JavaScriptHarddata_structuresalgorithmsarrays

Instructions

Create a function that given an array, it returns the index where if split in two-subarrays (last element of the first array has index of (foundIndex-1)), the sum of them are equal.

Examples

twins([10, 20, 30, 5, 40, 50, 40, 15]) ➞ 5
// foundIndex 5 : [10+20+30+5+40]=[50+40+15]

twins([1, 2, 3, 4, 5, 5]) ➞ 4
// [1, 2, 3, 4] [5, 5]

twins([3, 3]) ➞ 1

Notes

Return only the foundIndex, not the divided arrays.

javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.