-
Notifications
You must be signed in to change notification settings - Fork 47
/
Partition_subset.py
52 lines (39 loc) · 1.02 KB
/
Partition_subset.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
def findMinRec(arr, i, sumCalculated,
sumTotal):
# 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.
if (i == 0):
return abs((sumTotal - sumCalculated) -
sumCalculated)
# 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
return min(findMinRec(arr, i - 1,
sumCalculated+arr[i - 1],
sumTotal),
findMinRec(arr, i - 1,
sumCalculated, sumTotal))
# Returns minimum possible
# difference between sums
# of two subsets
def findMin(arr, n):
# Compute total sum
# of elements
sumTotal = 0
for i in range(n):
sumTotal += arr[i]
# Compute result using
# recursive function
return findMinRec(arr, n,
0, sumTotal)
# Driver code
if __name__ == "__main__":
arr = [3, 1, 4, 2, 2, 1]
n = len(arr)
print("The minimum difference " +
"between two sets is ",
findMin(arr, n))