Find all distinct subset ( or subsequence ) sums of an array | C # program to print distinct subset sums of a given array . ; sum denotes the current sum of the subset currindex denotes the index we have reached in the given array ; This function mainly calls recursive function distSumRec ( ) to generate distinct sum subsets . And finally prints the generated subsets . ; Print the result ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void distSumRec ( int [ ] arr , int n , int sum , int currindex , HashSet < int > s ) { if ( currindex > n ) return ; if ( currindex == n ) { s . Add ( sum ) ; return ; } distSumRec ( arr , n , sum + arr [ currindex ] , currindex + 1 , s ) ; distSumRec ( arr , n , sum , currindex + 1 , s ) ; } static void printDistSum ( int [ ] arr , int n ) { HashSet < int > s = new HashSet < int > ( ) ; distSumRec ( arr , n , 0 , 0 , s ) ; foreach ( int i in s ) Console . Write ( i + " ▁ " ) ; } public static void Main ( ) { int [ ] arr = { 2 , 3 , 4 , 5 , 6 } ; int n = arr . Length ; printDistSum ( arr , n ) ; } }