Find all distinct subset ( or subsequence ) sums of an 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
def distSumRec ( arr , n , sum , currindex , s ) : NEW_LINE INDENT if ( currindex > n ) : NEW_LINE INDENT return NEW_LINE DEDENT if ( currindex == n ) : NEW_LINE INDENT s . add ( sum ) NEW_LINE return NEW_LINE DEDENT distSumRec ( arr , n , sum + arr [ currindex ] , currindex + 1 , s ) NEW_LINE distSumRec ( arr , n , sum , currindex + 1 , s ) NEW_LINE DEDENT def printDistSum ( arr , n ) : NEW_LINE INDENT s = set ( ) NEW_LINE distSumRec ( arr , n , 0 , 0 , s ) NEW_LINE for i in s : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 3 , 4 , 5 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE printDistSum ( arr , n ) NEW_LINE DEDENT