Triplet sum code in python

Given a list of integers, write a Python program to find all triplets that sum up to given integer ‘k’.

Examples:

Input : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 10
Output : [(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]

Input : [12, 3, 6, 1, 6, 9], k = 24
Output : [(12, 6, 6), (12, 9, 3)]

Approach #1 : Naive (Using set)
In this approach, we use two for loops. The first loop sets first element, another to check whether other two elements including first sums up to k or not. This approach takes O(n2) time complexity.

def findTriplets(lst, k):

    triplet_count = 0

    final_temp_list =[]

    for i in range(0, len(lst)-1): 

        s = set() 

        temp_list = []

        temp_list.append(lst[i])

        curr_k = k - lst[i] 

        for j in range(i + 1, len(lst)): 

            if (curr_k - lst[j]) in s: 

                triplet_count += 1

                temp_list.append(lst[j])

                temp_list.append(curr_k - lst[j])

                final_temp_list.append(tuple(temp_list))

                temp_list.pop(2)

                temp_list.pop(1)

            s.add(lst[j])

    return final_temp_list

lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

k = 10

print(findTriplets(lst, k))

Output:

[(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]

 
Approach #2 : Using itertools
Python itertools module provide combination(iterable, r) function. This tool returns the r length subsequences of elements from the input iterable. Every time we make a combination of 3 elements and check if they sums up to k or not.

from itertools import combinations

def findTriplets(lst, key):

    def valid(val):

        return sum(val) == key

    return list(filter(valid, list(combinations(lst, 3))))

lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

print(findTriplets(lst, 10))

Output:

[(1, 2, 7), (1, 3, 6), (1, 4, 5), (2, 3, 5)]


How do you create a triplet in Python?

Practical Data Science using Python.
0 <= i < j < k < number of elements in nums..
|nums[i] - nums[j]| <= a..
|nums[j] - nums[k]| <= b..
|nums[i] - nums[k]| <= c..

How do you print triplets in Python?

Take three pointers i, j, k..
Initialize i with zero and start a nested loop for i..
Initialize j with (i+1) and start a nested loop for j..
Initialize k with (j+1) and start a loop for k..
If Target == arr[i] + arr[j] + arr[k] break the loop and print values of arr[i], arr[j], arr[k]..

What is triplet sum in array?

Your task is to find all the distinct triplets present in the array which adds up to a given number K. An array is said to have a triplet {ARR[i], ARR[j], ARR[k]} with sum = 'K' if there exists three indices i, j and k such that i!= j, j!= k and i!= j and ARR[i] + ARR[j] + ARR[k] = 'K'.

How do you find the sum of triplets in array?

Triplets can be found using the hashing technique..
Traverse the array from i = 0 to n - 2..
Create an empty hash table..
Traverse from j = i+ 1 to n -1..
sum = arr[i] + arr[j].
if (-sum) is present in the hash table,.
then print arr[i], arr[j] and -sum as triplets..
else, insert arr[j] in the hash table and proceed..