Equal-sum split is a prefix hit
Updated
A split after index i is equal when the left sum equals the right sum. Left sum is p[i]. Right sum is total−p[i]. Hit when p[i]==total/2 (total even). Skip the last index unless empty right is allowed.
What to focus on
Inclusive p[i]=a[0]+…+a[i]. Walk i, test 2*p[i]==total, and i<n-1. Odd total cannot hit.
Worked examples
hit after 1
a = [4, 1, 5], p = [4, 5, 10], total = 10- half 5
- p[1]=5
- left [4,1] right [5]
Result: split after i=1
mirror split
a = [1, 4, 5], p = [1, 5, 10], total = 10- p[1]=5
- left [1,4] right [5]
Result: split after i=1
odd total
a = [4, 1, 9, 1], p = [4, 5, 14, 15], total = 15- 15 is odd
- no i with 2*p[i]=15
Result: no equal split
Try it
Write equalSplit(a). a is the input array. Inclusive p[i] is the sum of a[0] through a[i]. Return the split index i where left equals right, or none. Skip the last index. [4, 1, 5] → split after i=1 [1, 4, 5] → split after i=1 [4, 1, 9, 1] → no equal split
a is a non-empty array of integers, length ≥ 2. Inclusive p has length n. p[i] covers a[0..i].