Skip to content

Commit 7a08d09

Browse files
Merge pull request TheAlgorithms#60 from dhruvsaini/patch-2
Create minimum_partition.py
2 parents ce77326 + 688a9ab commit 7a08d09

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""
2+
Partition a set into two subsets such that the difference of subset sums is minimum
3+
"""
4+
def findMin(arr):
5+
n = len(arr)
6+
s = sum(arr)
7+
8+
dp = [[False for x in range(s+1)]for y in range(n+1)]
9+
10+
for i in range(1, n+1):
11+
dp[i][0] = True
12+
13+
for i in range(1, s+1):
14+
dp[0][i] = False
15+
16+
for i in range(1, n+1):
17+
for j in range(1, s+1):
18+
dp[i][j]= dp[i][j-1]
19+
20+
if (arr[i-1] <= j):
21+
dp[i][j] = dp[i][j] or dp[i-1][j-arr[i-1]]
22+
23+
for j in range(s/2, -1, -1):
24+
if dp[n][j] == True:
25+
diff = s-2*j
26+
break;
27+
28+
return diff

0 commit comments

Comments
 (0)