MCPcopy Create free account
hub / github.com/subbarayudu-j/TheAlgorithms-Python / findMin

Function findMin

dynamic_programming/minimum_partition.py:4–28  ·  view source on GitHub ↗
(arr)

Source from the content-addressed store, hash-verified

2Partition a set into two subsets such that the difference of subset sums is minimum
3"""
4def 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(int(s/2), -1, -1):
24 if dp[n][j] == True:
25 diff = s-2*j
26 break;
27
28 return diff

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected