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
def printSets ( set1 , set2 ) : NEW_LINE INDENT for i in range ( 0 , len ( set1 ) ) : NEW_LINE INDENT print ( " { } ▁ " . format ( set1 [ i ] ) , end = " " ) ; NEW_LINE DEDENT print ( " " ) NEW_LINE for i in range ( 0 , len ( set2 ) ) : NEW_LINE INDENT print ( " { } ▁ " . format ( set2 [ i ] ) , end = " " ) ; NEW_LINE DEDENT print ( " " ) NEW_LINE DEDENT def findSets ( arr , n , set1 , set2 , sum1 , sum2 , pos ) : NEW_LINE INDENT if ( pos == n ) : NEW_LINE INDENT if ( sum1 == sum2 ) : NEW_LINE INDENT printSets ( set1 , set2 ) NEW_LINE return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT set1 . append ( arr [ pos ] ) NEW_LINE res = findSets ( arr , n , set1 , set2 , sum1 + arr [ pos ] , sum2 , pos + 1 ) NEW_LINE if ( res ) : NEW_LINE INDENT return res NEW_LINE DEDENT set1 . pop ( ) NEW_LINE set2 . append ( arr [ pos ] ) NEW_LINE res = findSets ( arr , n , set1 , set2 , sum1 , sum2 + arr [ pos ] , pos + 1 ) NEW_LINE if not res : NEW_LINE INDENT set2 . pop ( ) NEW_LINE DEDENT return res NEW_LINE DEDENT def isPartitionPoss ( arr , n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE DEDENT if ( sum % 2 != 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT set1 = [ ] NEW_LINE set2 = [ ] NEW_LINE return findSets ( arr , n , set1 , set2 , 0 , 0 , 0 ) NEW_LINE DEDENT arr = [ 5 , 5 , 1 , 11 ] NEW_LINE n = len ( arr ) NEW_LINE if ( isPartitionPoss ( arr , n ) == False ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT