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
[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.
#include <iostream>
#include <vector>
using namespace std;
bool findTriplet(vector<int>& arr) {
int n = arr.size();
// Iterate through all possible triplets
for (int i = 0; i < n - 2; i++) {
for (int j = i + 1; j < n - 1; j++) {
for (int k = j + 1; k < n; k++) {
// Check if sum of two elements
// equals the third element
if (arr[i] + arr[j] == arr[k] || arr[i] + arr[k] ==
arr[j] || arr[j] + arr[k] == arr[i]) {
return true;
}
}
}
}
return false;
}
// Driver Code
int main() {
vector<int> arr = {1, 2, 3, 4, 5};
if (findTriplet(arr))
cout << "true" << endl;
else
cout << "false" << endl;
return 0;
}
import java.util.*;
public class GfG {
public static boolean findTriplet(int[] arr) {
int n = arr.length;
// Iterate through all possible triplets
for (int i = 0; i < n - 2; i++) {
for (int j = i + 1; j < n - 1; j++) {
for (int k = j + 1; k < n; k++) {
// Check if sum of two elements
//equals the third element
if (arr[i] + arr[j] == arr[k] || arr[i] + arr[k] == arr[j] ||
arr[j] + arr[k] == arr[i]) {
return true;
}
}
}
}
return false;
}
// Driver Code
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
if (findTriplet(arr))
System.out.println("true");
else
System.out.println("false");
}
}
def findTriplet(arr):
n = len(arr)
# Iterate through all possible triplets
for i in range(n - 2):
for j in range(i + 1, n - 1):
for k in range(j + 1, n):
# Check if sum of two elements
# equals the third element
if arr[i] + arr[j] == arr[k] or arr[i] + arr[k] == arr[j] or arr[j] + arr[k] == arr[i]:
return True
return False
# Driver Code
arr = [1, 2, 3, 4, 5]
if findTriplet(arr):
print("true")
else:
print("false")
using System;
using System.Linq;
class GfG {
static bool findTriplet(int[] arr) {
int n = arr.Length;
// Iterate through all possible triplets
for (int i = 0; i < n - 2; i++) {
for (int j = i + 1; j < n - 1; j++) {
for (int k = j + 1; k < n; k++) {
// Check if sum of two elements
// equals the third element
if (arr[i] + arr[j] == arr[k] || arr[i] + arr[k] == arr[j] || arr[j] + arr[k] == arr[i]) {
return true;
}
}
}
}
return false;
}
// Driver Code
static void Main() {
int[] arr = {1, 2, 3, 4, 5};
if (findTriplet(arr))
Console.WriteLine("true");
else
Console.WriteLine("false");
}
}
function findTriplet(arr) {
const n = arr.length;
// Iterate through all possible triplets
for (let i = 0; i < n - 2; i++) {
for (let j = i + 1; j < n - 1; j++) {
for (let k = j + 1; k < n; k++) {
// Check if sum of two elements equals the third element
if (arr[i] + arr[j] === arr[k] || arr[i] + arr[k]
=== arr[j] || arr[j] + arr[k] === arr[i]) {
return true;
}
}
}
}
return false;
}
// Driver Code
const arr = [1, 2, 3, 4, 5];
if (findTriplet(arr))
console.log("true");
else
console.log("false");
Output
true
[Expected Approach] Two-Pointer Technique - O(n^2) time and O(1) space
We can sort the array and use two pointers to check whether two elements sum up to a third element.
After sorting the array, for each element arr[i], consider it as the third element of the triplet.
Use two pointers: left at the beginning of the array and right just before i.
- If
arr[left] + arr[right] == arr[i], a valid triplet exists. - If the sum is smaller than
arr[i], moveleftforward to increase the sum. - If the sum is greater than
arr[i], moverightbackward to decrease the sum.
#include <iostream>
#include <vector>
using namespace std;
bool findTriplet(vector<int>& arr) {
int n = arr.size();
// Sort the array
sort(arr.begin(), arr.end());
// Iterate through the array
for (int i = 2; i < n; i++) {
int left = 0, right = i - 1;
while (left < right) {
int sum = arr[left] + arr[right];
if (sum == arr[i])
return true;
else if (sum < arr[i])
left++;
else
right--;
}
}
return false;
}
// Driver Code with Predefined Input
int main() {
vector<int> arr = {1, 8, 5, 15, 10};
if (findTriplet(arr))
cout << "true" << endl;
else
cout << "false" << endl;
return 0;
}
import java.util.*;
public class GfG {
public static boolean findTriplet(int[] arr) {
int n = arr.length;
// Sort the array
Arrays.sort(arr);
// Iterate through the array
for (int i = 2; i < n; i++) {
int left = 0, right = i - 1;
while (left < right) {
int sum = arr[left] + arr[right];
if (sum == arr[i])
return true;
else if (sum < arr[i])
left++;
else
right--;
}
}
return false;
}
public static void main(String[] args) {
int[] arr = {1, 8, 5, 15, 10};
if (findTriplet(arr))
System.out.println("true");
else
System.out.println("false");
}
}
def findTriplet(arr):
n = len(arr)
# Sort the array
arr.sort()
# Iterate through the array
for i in range(2, n):
left = 0
right = i - 1
while left < right:
sum = arr[left] + arr[right]
if sum == arr[i]:
return True
elif sum < arr[i]:
left += 1
else:
right -= 1
return False
# Driver Code with Predefined Input
arr = [1, 8, 5, 15, 10]
if findTriplet(arr):
print('true')
else:
print('false')
using System;
public class GFG
{
public static bool findTriplet(int[] arr)
{
int n = arr.Length;
// Sort the array
Array.Sort(arr);
// Iterate through the array
for (int i = 2; i < n; i++)
{
int left = 0, right = i - 1;
while (left < right)
{
int sum = arr[left] + arr[right];
if (sum == arr[i])
return true;
else if (sum < arr[i])
left++;
else
right--;
}
}
return false;
}
public static void Main()
{
int[] arr = { 1, 8, 5, 15, 10 };
Console.WriteLine(findTriplet(arr).ToString().ToLower());
}
}
function findTriplet(arr) {
let n = arr.length;
// Sort the array
arr.sort((a, b) => a - b);
// Iterate through the array
for (let i = 2; i < n; i++) {
let left = 0, right = i - 1;
while (left < right) {
let sum = arr[left] + arr[right];
if (sum === arr[i])
return true;
else if (sum < arr[i])
left++;
else
right--;
}
}
return false;
}
// Driver Code with Predefined Input
let arr = [1, 8, 5, 15, 10];
if (findTriplet(arr))
console.log('true');
else
console.log('false');
Output
true