Python Program to Find a triplet such that sum of two equals to third element
Last Updated :
20 Apr, 2023
Given an array of integers, you have to find three numbers such that the sum of two elements equals the third element.
Examples:
Input: {5, 32, 1, 7, 10, 50, 19, 21, 2}
Output: 21, 2, 19
Input: {5, 32, 1, 7, 10, 50, 19, 21, 0}
Output: no such triplet exist
Question source: Arcesium Interview Experience | Set 7 (On campus for Internship)
Simple approach: Run three loops and check if there exists a triplet such that sum of two elements equals the third element.
Time complexity: O(n^3)
Efficient approach: The idea is similar to Find a triplet that sum to a given value.
- Sort the given array first.
- Start fixing the greatest element of three from the back and traverse the array to find the other two numbers which sum up to the third element.
- Take two pointers j(from front) and k(initially i-1) to find the smallest of the two number and from i-1 to find the largest of the two remaining numbers
- If the addition of both the numbers is still less than A[i], then we need to increase the value of the summation of two numbers, thereby increasing the j pointer, so as to increase the value of A[j] + A[k].
- If the addition of both the numbers is more than A[i], then we need to decrease the value of the summation of two numbers, thereby decrease the k pointer so as to decrease the overall value of A[j] + A[k].
Below image is a dry run of the above approach:

Below is the implementation of the above approach:
Python
# Python program to find three numbers
# such that sum of two makes the
# third element in array
# Utility function for finding
# triplet in array
def findTriplet(arr, n):
# Sort the array
arr.sort()
# For every element in arr check
# if a pair exist(in array) whose
# sum is equal to arr element
i = n - 1
while(i >= 0):
j = 0
k = i - 1
while (j < k):
if (arr[i] == arr[j] + arr[k]):
# Pair found
print "numbers are ", arr[i],
arr[j], arr[k]
return
elif (arr[i] > arr[j] + arr[k]):
j += 1
else:
k -= 1
i -= 1
# No such triplet is found in array
print "No such triplet exists"
# Driver code
arr = [5, 32, 1, 7, 10, 50, 19, 21, 2]
n = len(arr)
findTriplet(arr, n)
# This code is contributed by Sachin Bisht
Output:
numbers are 21 2 19
Time complexity: O(N^2)
Space Complexity: O(1) as no extra space has been used.
Please refer complete article on Find a triplet such that sum of two equals to third element for more details!
Similar Reads
Python3 Program to Find a triplet that sum to a given value Given an array and a value, find if there is a triplet in array whose sum is equal to the given value. If there is such a triplet present in array, then print the triplet and return true. Else return false.Examples: Input: array = {12, 3, 4, 1, 6, 9}, sum = 24; Output: 12, 3, 9 Explanation: There is
6 min read
Python3 Program to Find all triplets with zero sum Given an array of distinct elements. The task is to find triplets in the array whose sum is zero.Examples : Input : arr[] = {0, -1, 2, -3, 1}Output : (0 -1 1), (2 -3 1)Explanation : The triplets with zero sum are0 + -1 + 1 = 0 and 2 + -3 + 1 = 0 Input : arr[] = {1, -2, 1, 0, 5}Output : 1 -2 1Explana
6 min read
Python Program for Find a triplet from three linked lists with sum equal to a given number Given three linked lists, say a, b and c, find one node from each list such that the sum of the values of the nodes is equal to a given number. For example, if the three linked lists are 12->6->29, 23->5->8, and 90->20->59, and the given number is 101, the output should be triple "
4 min read
Python3 Program to Count triplets with sum smaller than a given value Given an array of distinct integers and a sum value. Find count of triplets with sum smaller than given sum value. The expected Time Complexity is O(n2).Examples: Input : arr[] = {-2, 0, 1, 3} sum = 2.Output : 2Explanation : Below are triplets with sum less than 2 (-2, 0, 1) and (-2, 0, 3) Input : a
3 min read
Python3 Program to Print all triplets in sorted array that form AP Given a sorted array of distinct positive integers, print all triplets that form AP (or Arithmetic Progression)Examples : Input : arr[] = { 2, 6, 9, 12, 17, 22, 31, 32, 35, 42 };Output :6 9 122 12 2212 17 222 17 3212 22 329 22 352 22 4222 32 42Input : arr[] = { 3, 5, 6, 7, 8, 10, 12};Output :3 5 75
3 min read