Count of subarrays having sum equal to its length
Explanation
- You need to remember
subarray(i, j) = prefix(i) - prefix(j)
- relation reduced to
prefix(i) - prefix(j) = i - j
- relation reduced to
prefix(i) - i = prefix(j) - j
- count how much similar no.s present, apply
nC2
, as 2 distinct points make a subarray. - corner case, count each one individually.