Write a Python program to print unique triplets whose three elements gives the sum of zero from an array of n integers.


Topic: Write a Python program to print unique triplets whose three elements gives the sum of zero from an array of n integers.

Solution

num = [1, -6, 4, 2, -1, 2, 0, -2, 0 ]
len_list = len(num)
trips = []
for i in range(len_list):
    if i+3 > len_list:
        break
    triplets = num[i:i+3]
    if len(set(triplets))==3:
        if sum(triplets) == 0:
            trips.append(triplets)
print(trips)



List all Python Programs