3728. Stable Subarrays With Equal Boundary and Interior Sum | LeetCode Solution
Raunit Verma - in Leetcode
Views: 11
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.
class Solution {
public:
long long countStableSubarrays(vector<int>& capacity) {
int n = capacity.size();
vector<long long>pre(n,0);
pre[0] = capacity[0];
for(int i = 1; i<n; i++){
pre[i] += pre[i-1] + capacity[i];
}
map<int,map<long long,long long>>m;
long long ans = 0;
for(int i = 1; i<n; i++){
long long currSum = pre[i];
int curr = capacity[i];
long long reqSum = currSum - curr * 2LL;
ans += m[curr][reqSum];
m[capacity[i-1]][pre[i-1]]++;
}
return ans;
}
};
Tagsleetcode3728. Stable Subarrays With Equal Boundary and Interior Sum
Raunit Verma
Technical Writer at CodingKaro
Share this on


