Open In App

JavaScript Program to find Smallest Difference Triplet from Three Arrays

Last Updated : 22 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The article is about finding the smallest difference triplet from three arrays in JavaScript. Given three sorted arrays, we need to find three elements (one from each array) such that the difference between the maximum and minimum among the three is minimized. In other words, we are looking for a triplet (a, b, c) where a is from the first array, b is from the second array, and c is from the third array, and (max(a, b, c) – min(a, b, c)) is minimized.

There are multiple approaches to finding the smallest difference triplet. Here are two common approaches:

Table of Content

Approach 1: Brute Force

  • In this approach, we use three nested loops to iterate through all possible combinations of elements from the three arrays.
  • For each combination, we calculate the maximum and minimum elements and find the difference.
  • We keep track of the minimum difference encountered so far and return the corresponding triplet.

Example: This example shows the use of the above-explained approach.

Javascript




function findSmallestDifferenceTriplet(arr1, arr2, arr3) {
    let minDifference = Number.MAX_SAFE_INTEGER;
    let resultTriplet;
  
    for (let i = 0; i < arr1.length; i++) {
        for (let j = 0; j < arr2.length; j++) {
            for (let k = 0; k < arr3.length; k++) {
                const maxNum = Math.max(
                    arr1[i],
                    arr2[j],
                    arr3[k]
                );
                const minNum = Math.min(
                    arr1[i],
                    arr2[j],
                    arr3[k]
                );
                const difference = maxNum - minNum;
  
                if (difference < minDifference) {
                    minDifference = difference;
                    resultTriplet = [
                        arr1[i],
                        arr2[j],
                        arr3[k],
                    ];
                }
            }
        }
    }
  
    return resultTriplet;
}
  
const arr1 = [1, 2, 3, 4, 7];
const arr2 = [5, 10, 12];
const arr3 = [8, 9, 11, 14];
console.log(
    findSmallestDifferenceTriplet(arr1, arr2, arr3)
);


Output

[ 7, 5, 8 ]

Approach 2: Merge and Track

  • This approach leverages the fact that the arrays are sorted.
  • We start with pointers at the beginning of each array.
  • At each step, we calculate the current difference and move the pointer of the array with the smallest element.
  • We continue this process until we exhaust any of the arrays, keeping track of the minimum difference encountered and the corresponding triplet.

Example: This example shows the use of the above-explained approach.

Javascript




function findSmallestDifferenceTriplet(arr1, arr2, arr3) {
    let i = 0;
    let j = 0;
    let k = 0;
    let minDifference = Number.MAX_SAFE_INTEGER;
    let resultTriplet;
  
    while (
        i < arr1.length &&
        j < arr2.length &&
        k < arr3.length
    ) {
        const maxNum = 
            Math.max(arr1[i], arr2[j], arr3[k]);
        const minNum = 
            Math.min(arr1[i], arr2[j], arr3[k]);
        const difference = maxNum - minNum;
  
        if (difference < minDifference) {
            minDifference = difference;
            resultTriplet = [arr1[i], arr2[j], arr3[k]];
        }
  
        if (arr1[i] === minNum) {
            i++;
        } else if (arr2[j] === minNum) {
            j++;
        } else {
            k++;
        }
    }
  
    return resultTriplet;
}
  
const arr1 = [1, 2, 3, 4, 7];
const arr2 = [5, 10, 12];
const arr3 = [8, 9, 11, 14];
console.log(
    findSmallestDifferenceTriplet(arr1, arr2, arr3)
);


Output

[ 7, 5, 8 ]


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads