Answer to Question #197012 in Python for Hari nadh babu

Question #197012
Triplet Sum

Write a Python Program for Triplet Sum

The below link contains Triplet Sum Question and test cases

https://drive.google.com/file/d/1kPR1eOaf9JTOqqAj4w_A4e8Nc0jIa3fM/view?usp=sharing
1
Expert's answer
2021-05-23T21:02:54-0400
def func(arr:list, n:int):
    st = set()

    for i in range(len(arr)):
        for j in range(i, len(arr)):
            for k in range(j, len(arr)):
                if i != j and j != k and k != i:
                    if arr[i] + arr[j] + arr[k] == n:
                        st.add((arr[i], arr[j], arr[k]))
    if (len(st) == 0):
        print('No Matching Triplets Found')
    else:
        for item in sorted(st):
            print(item)


if __name__=='__main__':
    # read an array
    arr = list(map(int, input().split()))
    arr.sort()
    # read a value
    n = int(input())
    
    func(arr, n)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS