FastPrepFastPrep
Problem Brief

Count Stable Segments

INTERNNEW GRADOA

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𓂃 𓈒𓏸

1Example 1

Input
capacity = [9, 3, 3, 3, 9]
Output
2
Explanation
Example 1 illustration
There are 2 stable subsegments: [3, 3, 3] and [9, 3, 3, 3, 9]. Return 2.

2Example 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

Limits and guarantees your solution can rely on.

  • 1 ≤ n ≤ 10^5
  • 1 <= capacity[i] <= 10^5
public int countStableSegments(int[] capacity) {
  // write your code here
}
Input

capacity

[9, 3, 3, 3, 9]

Output

2

Sign in to submit your solution.