Skip to content

Commit e428051

Browse files
authored
partition
Initial File
1 parent 3f47068 commit e428051

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

partition

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
def partition(arr,size):
2+
sum=0
3+
for i in range(size):
4+
sum=sum+arr[i]
5+
if sum%2!=0:
6+
return False
7+
8+
9+
subset=[[False for i in range(sum//2 + 1)]
10+
for i in range(size + 1)]
11+
12+
for i in range(size+1):
13+
subset[i][0]=True
14+
for j in range(sum//2 + 1):
15+
subset[0][j]=False
16+
17+
for i in range(1,size+1):
18+
for j in range(1,sum//2+1):
19+
if j < set[i-1]:
20+
subset[i][j]=subset[i-1][j]
21+
22+
if j >= set[i-1]:
23+
subset[i][j]= (subset[i-1][j] or subset[i-1][j-set[i-1]])
24+
return subset[size][sum//2]
25+
26+
set = [2, 2, 4, 5]
27+
sum = 20
28+
size = len(set)
29+
if partition(set, size) == True:
30+
print("True")
31+
else:
32+
print("False")

0 commit comments

Comments
 (0)