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 . has ( currsum - sum ) ) res += prevSum . get ( currsum - sum ) ;