Stable Subarrays With Equal Boundary and Interior Sum

You are given an integer array capacity.

A subarray capacity[l..r] is considered stable if:

  • Its length is at least 3.
  • The first and last elements are each equal to the sum of all elements strictly between them, i.e., capacity[l] = capacity[r] = capacity[l + 1] + capacity[l + 2] + ... + capacity[r - 1].

Return an integer denoting the number of stable subarrays.

Example 1
Inputcapacity = [9,3,3,3,9]
Output2
[9,3,3,3,9] is stable because both boundary elements are 9 and the interior sum is 9, and [3,3,3] is stable because both boundary elements are 3 and the interior sum is 3.
Example 2
Inputcapacity = [1,2,3,4,5]
Output0
No subarray of length at least 3 has equal first and last elements, so the answer is 0.

Constraints

  • 3 <= capacity.length <= 10^5
  • -10^9 <= capacity[i] <= 10^9

Asked at 2 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate