Balance The Array
Given an array of integers, find if it is possible to split it into two subarrays (not necessarily contiguous) such that the sum of the elements in one subarray is equal to the sum of the elements in the other subarray. Return true if it's possible, otherwise return false.
[ [ 1, 2, 3, 4 ] ]
Explanation. The array can be split into subarrays [1, 3] and [2, 4] both having the sum of 4.
[ [ 2, 1, 1 ] ]
Explanation. The array can be split into subarrays [2] and [1, 1], both having the sum of 2.
[ [ 10, 20, 15 ] ]
Explanation. It is not possible to split the array into two subarrays with equal sums in this case.
Follow-up: How would your solution change if the subarrays need to be contiguous?
The array will contain at least two elements. All elements are integers.
- Views
- 4