Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
JavaScript Program to Find a triplet such that sum of two equals to third element
We will be writing a JavaScript program that finds a triplet where the sum of two elements equals the third element. This program will be implemented using arrays and loop structures. We will iterate through the array and check for each combination of three elements if two elements sum up to the third. If we find such a triplet, we will return it immediately.
Problem Understanding
Given an array, we need to find three elements (a triplet) where a + b = c. For example, in array [1, 4, 45, 6, 10, 8], the triplet [4, 6, 10] satisfies this condition since 4 + 6 = 10.
Approach
Here is the approach to solve this problem:
Use three nested loops to generate all possible combinations of three elements
For each triplet, check if the sum of any two elements equals the third
Check all three possible combinations:
arr[i] + arr[j] = arr[k],arr[i] + arr[k] = arr[j], andarr[j] + arr[k] = arr[i]Return the first valid triplet found
If no triplet is found, return an appropriate message
Example
Here is a complete JavaScript program to find a triplet such that the sum of two elements equals the third element:
function findTriplet(arr) {
for (let i = 0; i < arr.length; i++) {
for (let j = i + 1; j < arr.length; j++) {
for (let k = j + 1; k < arr.length; k++) {
// Check all three possible combinations
if (arr[i] + arr[j] === arr[k]) {
return [arr[i], arr[j], arr[k]];
}
if (arr[i] + arr[k] === arr[j]) {
return [arr[i], arr[k], arr[j]];
}
if (arr[j] + arr[k] === arr[i]) {
return [arr[j], arr[k], arr[i]];
}
}
}
}
return "No such triplet found";
}
// Test the function
let arr = [1, 4, 45, 6, 10, 8];
let result = findTriplet(arr);
console.log("Array:", arr);
console.log("Triplet found:", result);
// Test with another array
let arr2 = [3, 34, 4, 12, 5, 2];
let result2 = findTriplet(arr2);
console.log("Array:", arr2);
console.log("Triplet found:", result2);
Array: [ 1, 4, 45, 6, 10, 8 ] Triplet found: [ 4, 6, 10 ] Array: [ 3, 34, 4, 12, 5, 2 ] Triplet found: [ 3, 4, 12 ]
Optimized Approach Using Sorting
For better efficiency, we can sort the array first and use a more optimized approach:
function findTripletOptimized(arr) {
// Sort array first
arr.sort((a, b) => a - b);
for (let i = arr.length - 1; i >= 2; i--) {
let left = 0;
let right = i - 1;
while (left < right) {
if (arr[left] + arr[right] === arr[i]) {
return [arr[left], arr[right], arr[i]];
} else if (arr[left] + arr[right] < arr[i]) {
left++;
} else {
right--;
}
}
}
return "No such triplet found";
}
// Test the optimized function
let arr3 = [1, 4, 45, 6, 10, 8];
let result3 = findTripletOptimized([...arr3]); // Use spread to avoid modifying original
console.log("Optimized result:", result3);
Optimized result: [ 4, 6, 10 ]
Comparison
| Approach | Time Complexity | Space Complexity | Advantages |
|---|---|---|---|
| Brute Force | O(n³) | O(1) | Simple implementation, finds all combinations |
| Optimized (Sorted) | O(n² + n log n) | O(1) | More efficient for larger arrays |
Key Points
The brute force approach checks all possible combinations of three elements
We need to check all three sum combinations: a+b=c, a+c=b, and b+c=a
The optimized approach uses sorting and two-pointer technique for better performance
Both methods return the first valid triplet found
Conclusion
This program successfully finds triplets where the sum of two elements equals the third. The brute force method is simple but has O(n³) complexity, while the optimized approach reduces this to O(n²) after sorting.
