← Back to challenges

Balanced Array

JavaScriptHardarraysconditionsmath

Instructions

Given an array of unknown length, but with an even amount of elements (numbers), copy the half with the highest sum of numbers to the other half of the array. If the sum of the first half equals the sum of the second half, return the original array.

Examples

balanced([1, 2, 4, 6, 3, 1]) ➞ [6, 3, 1, 6, 3, 1]

balanced([88, 3, 27, 5, 9, 0, 13, 10]) ➞ [88, 3, 27, 5, 88, 3, 27, 5]

balanced([7, 5, 2, 6, 1, 0, 1, 5, 2, 7, 0, 6]) ➞ [7, 5, 2, 6, 1, 0, 1, 5, 2, 7, 0, 6]

Notes

Each array has an even amount of elements.

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