human
Maximum possible difference of sum of two subsets of an array | Set 2 | Python 3 Program for the above approach.
Stores the positive elements.
Stores the negative elements.
Stores the count of 0 s.
Sum of all positive numbers.
Sum of all negative numbers.
Iterate over the array.
Stores the difference.
Sort the positive numbers in ascending order.
Sort the negative numbers in decreasing order.
Case 1 : Include both positive and negative numbers.
Put all numbers in subset A and one 0 in subset B.
Put all numbers in subset A except the smallest positive number which is put in B.
Put all numbers in subset B and one 0 in subset A.
Place the largest negative number in subset A and remaining in B.
Driver code"
Solution in Python:
 ... Can you provide a code to maximum possible difference of sum of two subsets of an array and the other steps required?
assistant
#Here is code to maximum possible difference of sum of two subsets of an array and the other steps required.
 def maxSumAfterPartition ( arr , n ) :
	pos = [ ]
	neg = [ ]
	zero = 0
	pos_sum = 0
	neg_sum = 0
	for i in range ( n ) :
		if ( arr [ i ] > 0 ) :
			pos . append ( arr [ i ] )
			pos_sum += arr [ i ]
		elif ( arr [ i ] < 0 ) :
			neg . append ( arr [ i ] )
			neg_sum += arr [ i ]
		else :
			zero += 1
	ans = 0
	pos . sort ( )
	neg . sort ( reverse = True )
	if ( len ( pos ) > 0 and len ( neg ) > 0 ) :
		ans = ( pos_sum - neg_sum )
	elif ( len ( pos ) > 0 ) :
		if ( zero > 0 ) :
			ans = ( pos_sum )
		else :
			ans = ( pos_sum - 2 * pos [ 0 ] )
	else :
		if ( zero > 0 ) :
			ans = ( - 1 * neg_sum )
		else :
			ans = ( neg [ 0 ] - ( neg_sum - neg [ 0 ] ) )
	return ans
if __name__ == ' _ _ main _ _ ' :
	arr = [ 1 , 2 , 3 , - 5 , - 7 ]
	n = len ( arr )
	print ( maxSumAfterPartition ( arr , n ) )