Description
Solutions
Count Stable Segments
🀘 INTERNπŸ‘©β€πŸŽ“ NEW GRAD

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:

  1. 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
Constraints:
    • 1 ≀ n ≀ 10^5
    • 1 <= capacity[i] <= 10^5
Thumbnail 0
Testcase

Result
Case 1

input:

output: