Solved Problem - Triplet Sum
We'll cover the following
Problem statement
Given a sorted array of integers . Find if there exists a triplet of integers , and such that the sum is equal to given integer .
Input format
The first line of input consists of two space-separated integers and .
The next line contains space-separated integers representing the array .
Output format
Print a single line of three space-separated integers , and such that or print if there is no such triplet.
Sample
Input
6 35
2 4 7 10 15 16
Output
2 5 6
Explanation
, and element of add upto .
Brute force
Use three loops to iterate over the array and check all pairs. Time complexity is .
Optimize to
The algorithm is a slight modification of the Pair sum problem discussed earlier. If we iterate over the array for then we need to find a pair in the array that adds to .
The second part is exactly the previous problem.
Get hands-on with 1400+ tech skills courses.