Find Sum of all unique sub | C # for finding sum of all unique subarray sum ; function for finding grandSum ; Go through all subarrays , compute sums and count occurrences of sums . ; Print all those sums that appear once . ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int findSubarraySum ( int [ ] arr , int n ) { int res = 0 ; 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 ) ; } } } foreach ( KeyValuePair < int , int > x in m ) if ( x . Value == 1 ) res += x . Key ; return res ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 2 , 3 , 1 , 4 } ; int n = arr . Length ; Console . WriteLine ( findSubarraySum ( arr , n ) ) ; } }