Go through all subarrays , compute sums and count occurrences of sums .
Dictionary < int , int > m = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int sum = 0 ; for ( int j = i ; j < n ; j ++ ) { sum += arr [ j ] ; if ( m . ContainsKey ( sum ) ) { m [ sum ] = m [ sum ] + 1 ; } else { m . Add ( sum , 1 ) ; } } }