while(begin < n) { while(end < n && !s.count(prefix[end + 1])) { ++end; s.insert(prefix[end]); } ans += end - begin; s.erase(prefix[begin]); ++begin; } code example
Example: build a prefix array cpp
void fillPrefixSum(int arr[], int n, int prefixSum[])
{
prefixSum[0] = arr[0];
// Adding present element
// with previous element
for (int i = 1; i < n; i++)
prefixSum[i] = prefixSum[i - 1] + arr[i];
}