forked from kumaya/python-programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtriplets.py
49 lines (45 loc) · 1.7 KB
/
triplets.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
#Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
#Note: Elements in a triplet (a,b,c) must be in non-descending order. (ie, a <= b <= c) The solution set must not contain duplicate triplets.
class triplets:
def __init__(self, inpArr):
self.inpArr = inpArr
def removeDuplicate(self, tempInp):
finalOutput = []
for x in tempInp:
if x not in finalOutput:
finalOutput.append(x)
if len(finalOutput)==0:
return "Empty"
else:
print "Final triplets where sum = 0:"
return finalOutput
def solution(self):
finalOutput1 = []
a = 0
sortedInp = sorted(self.inpArr)
for a,val in enumerate(sortedInp):
b = a + 1
c = len(sortedInp) - 1
if sortedInp[c] < 0:
break
while (b < c):
sumTriplet = sortedInp[a] + sortedInp[b] + sortedInp[c]
if sumTriplet < 0:
b += 1
elif sumTriplet > 0:
c -= 1
else:
tempOutput = []
tempOutput.append(sortedInp[a])
tempOutput.append(sortedInp[b])
tempOutput.append(sortedInp[c])
finalOutput1.append(tempOutput)
b += 1
c -= 1
finalOutput = self.removeDuplicate(finalOutput1)
print finalOutput
if __name__:
inpArr = [1,6,0,2,0,-1,-1,4,-6]
# inpArr = [1,2,3,4,0]
obj = triplets(inpArr)
obj.solution()