Count how many ways to split an array into three contiguous parts with equal sum.
You are given an array of integers. Count the number of ways to choose two cut points so that the array is split into three non-empty contiguous parts, and the sum of each part is the same.
More formally, for an array of length , count pairs such that:
- the subarray , , and all have the same sum
Return the number of valid splits.
Input Format
- The first line contains an integer .
- The second line contains integers .
Output Format
- Print a single integer: the number of valid ways to split the array into three non-empty contiguous parts with equal sum.
Constraints
- Array values may be negative, zero, or positive.
- The answer may be large; use a 64-bit integer.
- In the original contest setting, the count is computed exactly without needing modulo unless specified by the platform statement.
Example 1
Input
5 1 2 3 0 3
Output
2
Explanation
The total sum is 9, so each part must sum to 3. The valid splits are [1,2] | [3] | [0,3] and [1,2] | [3,0] | [3].
Example 2
Input
4 0 0 0 0
Output
3
Explanation
Every part must sum to 0. The valid cut pairs are any two distinct cut points among the 3 available positions, so the number of ways is 3.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.