VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-triplet-sum-two-equals-third-element/

⇱ Check for Triplet with One as Sum of other Two - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Check for Triplet with One as Sum of other Two

Last Updated : 6 Apr, 2026

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.

[Naive Approach] Trying All Triplets One by One - O(n^3) time and O(1) space

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.


Output
true

[Expected Approach] Two-Pointer Technique - O(n^2) time and O(1) space

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 (left and right) while iterating through the array. This makes the approach more efficient compared to the naive method.



Output
true
Comment