3728. Stable Subarrays With Equal Boundary and Interior Sum Medium
1/**
2 * [3728] Stable Subarrays With Equal Boundary and Interior Sum
3 *
4 * You are given an integer array capacity.
5 * A <span data-keyword="subarray-nonempty">subarray</span> capacity[l..r] is considered stable if:
6 *
7 * Its length is at least 3.
8 * 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]).
9 *
10 * Return an integer denoting the number of stable subarrays.
11 *
12 * <strong class="example">Example 1:
13 * <div class="example-block">
14 * Input: <span class="example-io">capacity = [9,3,3,3,9]</span>
15 * Output: <span class="example-io">2</span>
16 * Explanation:
17 *
18 * [9,3,3,3,9] is stable because the first and last elements are both 9, and the sum of the elements strictly between them is 3 + 3 + 3 = 9.
19 * [3,3,3] is stable because the first and last elements are both 3, and the sum of the elements strictly between them is 3.
20 * </div>
21 * <strong class="example">Example 2:
22 * <div class="example-block">
23 * Input: <span class="example-io">capacity = [1,2,3,4,5]</span>
24 * Output: <span class="example-io">0</span>
25 * Explanation:
26 * No subarray of length at least 3 has equal first and last elements, so the answer is 0.
27 * </div>
28 * <strong class="example">Example 3:
29 * <div class="example-block">
30 * Input: <span class="example-io">capacity = [-4,4,0,0,-8,-4]</span>
31 * Output: <span class="example-io">1</span>
32 * Explanation:
33 * [-4,4,0,0,-8,-4] is stable because the first and last elements are both -4, and the sum of the elements strictly between them is 4 + 0 + 0 + (-8) = -4
34 * </div>
35 *
36 * Constraints:
37 *
38 * 3 <= capacity.length <= 10^5
39 * -10^9 <= capacity[i] <= 10^9
40 *
41 */
42pub struct Solution {}
43
44// problem: https://leetcode.com/problems/stable-subarrays-with-equal-boundary-and-interior-sum/
45// discuss: https://leetcode.com/problems/stable-subarrays-with-equal-boundary-and-interior-sum/discuss/?currentPage=1&orderBy=most_votes&query=
46
47// submission codes start here
48
49impl Solution {
50 pub fn count_stable_subarrays(capacity: Vec<i32>) -> i64 {
51
52 }
53}
54
55// submission codes end
56
57#[cfg(test)]
58mod tests {
59 use super::*;
60
61 #[test]
62 fn test_3728() {
63 }
64}
65Back
© 2026 bowen.ge All Rights Reserved.