From ec861eb6ee188c3ed4e5743a72078fa2a02a9461 Mon Sep 17 00:00:00 2001 From: anwesha sinha Date: Sat, 31 Oct 2020 21:08:54 +0530 Subject: [PATCH] Create triplet.py --- Algorithms/triplet.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Algorithms/triplet.py diff --git a/Algorithms/triplet.py b/Algorithms/triplet.py new file mode 100644 index 00000000..f31f3eba --- /dev/null +++ b/Algorithms/triplet.py @@ -0,0 +1,34 @@ +""" +A simple Python 3 program +to find three elements whose +sum is equal to zero + +Prints all triplets in +arr[] with 0 sum + +""" + +def findTriplets(arr, n): + + found = True + for i in range(0, n-2): + + for j in range(i+1, n-1): + + for k in range(j+1, n): + + if (arr[i] + arr[j] + arr[k] == 0): + print(arr[i], arr[j], arr[k]) + found = True + + + # If no triplet with 0 sum + # found in array + if (found == False): + print(" not exist ") + + +arr = [0, -1, 2, -3, 1] +n = len(arr) +findTriplets(arr, n) +