Skip to main content

Split Array Equal Sums

easy
Software engineer

Given an array of positive integers, find an index where the array can be split into two contiguous subarrays with equal sums.

The element at the split index belongs to the left subarray, while the element immediately after starts the right subarray. Formally, if the split occurs at index i, we check if: $$\sum_{j=0}^{i} array[j] = \sum_{k=i+1}^{n-1} array[k]$$

If no valid split index exists, return -1.

Example 1

Input

array = [1, 2, 1, 1, 3]

Output

2

Explanation

The total sum of the array is 8. If we split at index 2, the left subarray is [1, 2, 1] which sums to 4. The right subarray is [1, 3] which also sums to 4. Since the sums are equal, 2 is a valid split index.

Example 2

Input

array = [1, 1, 1, 1, 1, 5]

Output

4

Example 3

Input

array = [1, 2, 3, 4, 6]

Output

-1

Constraints

  • 2 <= array.length <= 10⁵
  • 1 <= array[i] <= 10⁴

Screening

ArrayPrefix SumSimulation
Language
Code editor loads in the browser.

Output

Input

[1,2,1,1,3]

Expected

2

Your output

Run to see your output.