![]() |
VOOZH | about |
Given an array arr[] of integers, write a function to find whether there exist three elements such that the sum of two elements is equal to the third element.
Examples:
Input: arr[] = [1, 2, 3, 4, 5]
Output: true
Explanation: The pair (1, 2) sums to 3.Input: arr[] = [3, 4, 5]
Output: false
Explanation: No triplets satisfy the condition.Input: arr[] = [1, 8, 5, 15, 10]
Output: true
Explanation: The pair (5,10) sums to 15.
Table of Content
This approach uses three nested loops to examine all possible triplets in the array. It checks if the sum of any two elements equals the third element and prints all such valid triplets.
true
This approach first sorts the array and then uses the two-pointer technique to find a triplet where the sum of two numbers equals the third number. Instead of checking all possible triplets using three loops, it reduces the search space by adjusting two pointers (
leftandright) while iterating through the array. This makes the approach more efficient compared to the naive method.
true