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. It checks if the sum of any two elements equals the third element and prints all such valid triplets.
#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;
}
#include <stdio.h>
#include <stdbool.h>
bool findTriplet(int arr[], int n) {
// 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() {
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);
if (findTriplet(arr, n))
printf("true\n");
else
printf("false\n");
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 find_triplet(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 find_triplet(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
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.
#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;
}
#include <stdio.h>
#include <stdlib.h>
// Comparator function for qsort
int compare(const void *a, const void *b) {
return (*(int*)a - *(int*)b);
}
int findTriplet(int arr[], int n) {
// Sort the array
qsort(arr, n, sizeof(int), compare);
// 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 1; // true
else if (sum < arr[i])
left++;
else
right--;
}
}
return 0; // false
}
// Driver Code
int main() {
int arr[] = {1, 8, 5, 15, 10};
int n = sizeof(arr) / sizeof(arr[0]);
if (findTriplet(arr, n))
printf("true\n");
else
printf("false\n");
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;
using System.Collections.Generic;
using System.Linq;
public class GfG
{
public static bool findTriplet(List<int> arr)
{
int n = arr.Count;
// Sort the array
arr.Sort();
// 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()
{
List<int> arr = new List<int>{1, 8, 5, 15, 10};
if (findTriplet(arr))
Console.WriteLine("true");
else
Console.WriteLine("false");
}
}
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