FastPrepCount Stable Server Subsegments

Count Stable Server Subsegments

Scale AI logoScale AI● MediumNEW GRADOA
Learn

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[]) → long

Examples

Example 1

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

The stable subsegments are [9,3,1,2,3,9] and [3,1,2,3].

Example 2

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

The two endpoints are both 3, and the interior sum is 1+2=3.

Example 3

capacity = [1,1,1]return = 1

The 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.

More Scale AI problems

See Scale AI hiring insights
public long countStableServerSegments(long[] capacity) {
    // write your code here
}
capacity[9,3,1,2,3,9,10]
expected2
Checking account…