Count Stable Server Subsegments
Problem statement
You are given an array capacity of positive server capacities.
A contiguous subsegment [l,r] is stable when it has length at least three and both endpoint capacities equal the sum of all interior capacities:
capacity[l] = capacity[r] = capacity[l+1] + ... + capacity[r-1].
Return the number of stable subsegments.
Function
countStableServerSegments(capacity: long[]) → longExamples
Example 1
capacity = [9,3,1,2,3,9,10]return = 2The stable subsegments are [9,3,1,2,3,9] and [3,1,2,3].
Example 2
capacity = [3,1,2,3]return = 1The two endpoints are both 3, and the interior sum is 1+2=3.
Example 3
capacity = [1,1,1]return = 1The only length-three subsegment satisfies 1=1=1.
Constraints
1 <= capacity.length <= 300000.1 <= capacity[i] <= 1000000000.- The answer fits in a signed 64-bit integer.