currsum exceeds given sum by currsum - sum . Find number of subarrays having this sum and exclude those subarrays from currsum by increasing count by same amount .
if ( prevSum . ContainsKey ( currsum - sum ) ) res += prevSum [ currsum - sum ] ;