human
What is a code to find all distinct subset ( or subsequence ) sums of an array and the other steps required given the following:
Find all distinct subset ( or subsequence ) sums of an array | Set | Function to print all th distinct sum.
Declare a boolean array of size equal to total sum of the array.
Fill the first row beforehand.
dp [ j ] will be true only if sum j can be formed by any possible addition of numbers in given array upto index i , otherwise false.
Iterate from maxSum to 1 and avoid lookup on any other row.
Do not change the dp array for j less than arr [ i ].
If dp [ j ] is true then print.
Function to find the total sum and print the distinct sum.
find the sum of array elements.
Function to print all the distinct sum.
Driver Code"
Solution in Python:
assistant
#Here is code to find all distinct subset ( or subsequence ) sums of an array and the other steps required.
 def subsetSum ( arr , n , maxSum ) :
	dp = [ False for i in range ( maxSum + 1 ) ]
	dp [ arr [ 0 ] ] = True
	for i in range ( 1 , n , 1 ) :
		j = maxSum
		while ( j >= 1 ) :
			if ( arr [ i ] <= j ) :
				if ( arr [ i ] == j or dp [ j ] or dp [ ( j - arr [ i ] ) ] ) :
					dp [ j ] = True
				else :
					dp [ j ] = False
			j -= 1
	print ( 0 , end = " ▁ " )
	for j in range ( maxSum + 1 ) :
		if ( dp [ j ] == True ) :
			print ( j , end = " ▁ " )
	print ( "21" )
def printDistinct ( a , n ) :
	maxSum = 0
	for i in range ( n ) :
		maxSum += a [ i ]
	subsetSum ( a , n , maxSum )
if __name__ == ' _ _ main _ _ ' :
	arr = [ 2 , 3 , 4 , 5 , 6 ]
	n = len ( arr )
	printDistinct ( arr , n )