Partition a set into two subsets such that the difference of subset sums is minimum | Function to find the minimum sum ; If we have reached last element . Sum of one subset is sumCalculated , sum of other subset is sumTotal - sumCalculated . Return absolute difference of two sums . ; For every item arr [ i ] , we have two choices ( 1 ) We do not include it first set ( 2 ) We include it in first set We return minimum of two choices ; Returns minimum possible difference between sums of two subsets ; Compute total sum of elements ; Compute result using recursive function ; Driver code
def findMinRec ( arr , i , sumCalculated , sumTotal ) : NEW_LINE INDENT if ( i == 0 ) : NEW_LINE INDENT return abs ( ( sumTotal - sumCalculated ) - sumCalculated ) NEW_LINE DEDENT return min ( findMinRec ( arr , i - 1 , sumCalculated + arr [ i - 1 ] , sumTotal ) , findMinRec ( arr , i - 1 , sumCalculated , sumTotal ) ) NEW_LINE DEDENT def findMin ( arr , n ) : NEW_LINE INDENT sumTotal = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sumTotal += arr [ i ] NEW_LINE DEDENT return findMinRec ( arr , n , 0 , sumTotal ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 3 , 1 , 4 , 2 , 2 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " The ▁ minimum ▁ difference ▁ " + " between ▁ two ▁ sets ▁ is ▁ " , findMin ( arr , n ) ) NEW_LINE DEDENT