AmazonOA #13
Get Total Balanced
Estimated MediumJudge-ready
Topics (AI-labelled)Prefix & RangesHashing
You are given an array of positive integer capacities. A contiguous segment of length at least 3 is balanced if and only if its left-end capacity, its right-end capacity, and the sum of all capacities between the two ends are all equal. Find the number of balanced segments.
Input
The first line contains n (1..100000), and the second line contains n capacities (1..10⁹).
Output
Output one integer, the answer required by the problem.
Example 1
Input:
5
9 3 3 3 9
Output:
2
Example 2
Input:
2
1 1
Output:
0
Example 3
Input:
3
2 2 2
Output:
1
Example notes
Example 1 contains two balanced segments: the whole [9,3,3,3,9] and the middle [3,3,3].
Editorial
Loading solution…