human
Print equal sum sets of array ( Partition problem ) | Set 1 | Function to print the equal sum sets of the array ..
Print set 1..
Print set 2..
Utility function to find the sets of the array which have equal sum ..
If entire array is traversed , compare both the sums ..
If sums are equal print both sets and return true to show sets are found ..
If sums are not equal then return sets are not found ..
Add current element to set 1..
Recursive call after adding current element to set 1..
If this inclusion results in an equal sum sets partition then return true to show desired sets are found ..
If not then backtrack by removing current element from set1 and include it in set 2..
Recursive call after including current element to set 2 and removing the element from set 2 if it returns False.
Return true if array arr can be partitioned into two equal sum sets or not ..
Calculate sum of elements in array ..
If sum is odd then array cannot be partitioned ..
Declare vectors to store both the sets ..
Find both the sets ..
Driver code"
Solution in Python:
assistant
#Here is code to print equal sum sets of array ( Partition problem ) and the other steps required.
 def printSets ( set1 , set2 ) :
	for i in range ( 0 , len ( set1 ) ) :
		print ( " { } ▁ " . format ( set1 [ i ] ) , end = " " ) ;
	print ( " " )
	for i in range ( 0 , len ( set2 ) ) :
		print ( " { } ▁ " . format ( set2 [ i ] ) , end = " " ) ;
	print ( " " )
def findSets ( arr , n , set1 , set2 , sum1 , sum2 , pos ) :
	if ( pos == n ) :
		if ( sum1 == sum2 ) :
			printSets ( set1 , set2 )
			return True
		else :
			return False
	set1 . append ( arr [ pos ] )
	res = findSets ( arr , n , set1 , set2 , sum1 + arr [ pos ] , sum2 , pos + 1 )
	if ( res ) :
		return res
	set1 . pop ( )
	set2 . append ( arr [ pos ] )
	res = findSets ( arr , n , set1 , set2 , sum1 , sum2 + arr [ pos ] , pos + 1 )
	if not res :
		set2 . pop ( )
	return res
def isPartitionPoss ( arr , n ) :
	sum = 0
	for i in range ( 0 , n ) :
		sum += arr [ i ]
	if ( sum % 2 != 0 ) :
		return False
	set1 = [ ]
	set2 = [ ]
	return findSets ( arr , n , set1 , set2 , 0 , 0 , 0 )
arr = [ 5 , 5 , 1 , 11 ]
n = len ( arr )
if ( isPartitionPoss ( arr , n ) == False ) :
	print ( " - 1" )