Balanced Array
Given an array of non-negative integers, determine if the array can be split into two parts such that the sum of elements in both parts is the same. Return true if it can be split equally, otherwise return false.
[ [ 1, 5, 11, 5 ] ]
Explanation. The array can be split into [1, 5, 5] and [11] where both have the sum of 11.
[ [ 1, 2, 3, 5 ] ]
Explanation. There is no possible way to split this array into two parts with an equal sum.
[ [ 10, 10, 10, 10 ] ]
Explanation. The array can be split into [10, 10] and [10, 10] where both have the sum of 20.
[ [ 1, 1, 1, 2, 1 ] ]
Explanation. The array can be split into [1, 1, 1, 1] and [2] where both have the sum of 4.
[ [ 2, 3, 1, 1, 2, 1 ] ]
Explanation. There is no possible way to split this array into two parts with an equal sum.
Follow-up: Can you solve the problem in linear time O(n) and constant space O(1)?
The input array will contain at least one element and all elements will be non-negative integers.
- Views
- 3