Stable Subarrays With Equal Boundary and Interior Sum
You are given an integer array capacity.
A subarray capacity[l..r] is considered stable if:
- Its length is at least 3.
- The first and last elements are each equal to the sum of all elements strictly between them, i.e.,
capacity[l] = capacity[r] = capacity[l + 1] + capacity[l + 2] + ... + capacity[r - 1].
Return an integer denoting the number of stable subarrays.
Example 1
Input
capacity = [9,3,3,3,9]Output
2[9,3,3,3,9] is stable because both boundary elements are 9 and the interior sum is 9, and [3,3,3] is stable because both boundary elements are 3 and the interior sum is 3.
Example 2
Input
capacity = [1,2,3,4,5]Output
0No subarray of length at least 3 has equal first and last elements, so the answer is 0.
Constraints
- 3 <= capacity.length <= 10^5
- -10^9 <= capacity[i] <= 10^9