summaryrefslogtreecommitdiff
path: root/AlgoDesignAndTechniqueEdxPython/sources/partition3.py
diff options
context:
space:
mode:
Diffstat (limited to 'AlgoDesignAndTechniqueEdxPython/sources/partition3.py')
-rw-r--r--AlgoDesignAndTechniqueEdxPython/sources/partition3.py43
1 files changed, 43 insertions, 0 deletions
diff --git a/AlgoDesignAndTechniqueEdxPython/sources/partition3.py b/AlgoDesignAndTechniqueEdxPython/sources/partition3.py
new file mode 100644
index 0000000..9a711f1
--- /dev/null
+++ b/AlgoDesignAndTechniqueEdxPython/sources/partition3.py
@@ -0,0 +1,43 @@
+# Uses python3
+import sys
+
+
+def getPartition3(a):
+ if len(a) < 3:
+ return 0
+ sum = 0
+ for i in a:
+ sum = sum + i;
+ if sum % 3 != 0:
+ return 0
+ W = int(sum / 3)
+ values = optimalWeight2DArray(W, a)
+ count = 0
+ for i in range(1, len(a) + 1):
+ for w in range(1, W + 1):
+ if values[w][i] == W:
+ count = count + 1
+ if count >= 3:
+ return 1
+ else:
+ return 0
+
+
+def optimalWeight2DArray(W, w2):
+ j = len(w2)
+ value = [[0 for _ in range(j + 1)] for _ in range(W + 1)]
+
+ for i in range(1, j + 1):
+ for w in range(1, W + 1):
+ value[w][i] = value[w][i - 1]
+ if w2[i - 1] <= w:
+ val = value[w - w2[i - 1]][i - 1] + w2[i - 1]
+ if value[w][i] < val:
+ value[w][i] = val
+ return value
+
+
+if __name__ == '__main__':
+ input = sys.stdin.read()
+ n, *A = list(map(int, input.split()))
+ print(getPartition3(A))