FastPrepCount Stable Segments

Count Stable Segments

Citadel logoCitadelMediumINTERNNEW GRADOA
Learn

Problem statement

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

countStableSegments(capacity: int[]) → int

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

Examples

Example 1

capacity = [9, 3, 3, 3, 9]return = 2
Example 1 illustration

There are 2 stable subsegments: [3, 3, 3] and [9, 3, 3, 3, 9]. Return 2.

Example 2

capacity = [9, 3, 1, 2, 3, 9, 10]return = 2

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

More Citadel problems

See Citadel hiring insights
public int countStableSegments(int[] capacity) {
  // write your code here
}
capacity[9, 3, 3, 3, 9]
expected2
Checking account…