JavaScript Program to Find a triplet that sum to a given value


We are going to write a JavaScript program to find a triplet that sum up to a given value. This program will make use of nested loops to iterate over the input array and check for the existence of a triplet with a sum equal to the given value. Our program will continuously search for a triplet until it is found or all possible combinations have been exhausted. This program will make it possible for us to find a triplet that sum up to a given value in an efficient and straightforward manner.

Approach

The approach to find a triplet that sums up to a given value can be achieved using the following steps −

  • Sort the input array in ascending order.

  • Loop through the array and fix one element

  • Initialize two pointers, one pointing to the next element of the fixed element, and the other pointing to the end of the array.

  • Check if the sum of the three elements is equal to the given value.

  • If the sum is less than the given value, increment the left pointer.

  • If the sum is greater than the given value, decrement the right pointer. Repeat the process until either a triplet is found or the pointers cross each other.

Example

Given an array of integers, we want to find a triplet that sum to a given value. Here's a JavaScript program that solves this problem −

function findTriplet(arr, sum) {
   
   // First, we sort the array in ascending order
   arr.sort((a, b) => a - b);
     
   // Next, we iterate through the array with the outer loop
   for (let i = 0; i < arr.length - 2; i++) {
      
      // We start the inner loop from i + 1 to avoid using the same number twice
      let left = i + 1;
      let right = arr.length - 1;
         
      // The inner loop moves the left and right pointers towards each other
      while (left < right) {
         
         // If the sum of the current triplet is equal to the given sum, we have found our  solution
         if (arr[i] + arr[left] + arr[right] === sum) {
            return [arr[i], arr[left], arr[right]];
         }
         
         // If the sum of the current triplet is less than the given sum, we need to increase the sum
         
         // So, we move the left pointer to the right
         else if (arr[i] + arr[left] + arr[right] < sum) {
            left++;
         }
         
         // If the sum of the current triplet is greater than the given sum, we need to decrease the sum
         
         // So, we move the right pointer to the left
         else {
            right--;
         }
      }
   }
    
   // If no triplet is found, we return null
   return null;
}

// Example usage
let arr = [1, 4, 45, 6, 10, 8];
let sum = 22;
let triplet = findTriplet(arr, sum);
console.log(triplet); 

Explanation

  • The function findTriplet takes in an array arr and a sum as arguments.

  • First, the array is sorted in ascending order using the sort method.

  • Next, we iterate through the array with the outer loop, using for loop with the variable i starting from 0 to arr.length - 2.

  • Within the outer loop, we start the inner loop from i + 1 to avoid using the same number twice. Two pointers left and right are initialized with i + 1 and arr.length - 1, respectively.

  • In the inner loop, we use a while loop to keep moving the left and right pointers towards each other until left is less than right.

  • Within the while loop, we check the current sum of the triplet arr[i] + arr[left] + arr[right].

  • If it's equal to the given sum, we have found our solution and we return the triplet [arr[i], arr[left], arr[right]].

  • If it's less than the given sum, we need to increase the sum, so we move the left pointer to the right by incrementing left.

  • If it's greater than the given sum, we need to decrease the sum

Updated on: 13-Mar-2023

205 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements