Open In App

JavaScript Program to find Smallest Difference Triplet from Three Arrays

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

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




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

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




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 ]

Article Tags :