In an organization, there are n servers, each with a capacity of capacity[i]. A contiguous subsegment[l, r] of servers is said to be stable if capacity[i] = capacity[r] = sum[l + 1, r - 1]. In other words, the capacity of the servers at the endpoints of the segment should be equal to the sum the sum of the capacities of all the interior servers.
Find the num of stable sub-segments of length 3 or more.
Function Description
Complete the function countStableSegments
in the editor.
countStableSegments
has the following parameters:
int[] capacity
: the capacity of each server
Returns
int
: the num of stable segments
Κΰ¬ Many thanks to Aura Manπ ππΈ
Example 1:
Input: capacity = [9, 3, 3, 3, 9]
Output: 2
Explanation:There are 2 stable subsegments: [3, 3, 3] and [9, 3, 3, 3, 9]. Return 2.
Example 2:
Input: capacity = [9, 3, 1, 2, 3, 9, 10]
Output: 2
Explanation:The stable segments are [9, 3, 1, 2, 3, 9] and [3, 1, 2, 3] :D
1 β€ n β€ 10^5
1 <= capacity[i] <= 10^5
input:
output: