Open In App

JavaScript Program to find Smallest Difference Triplet from Three Arrays

Last Updated : 26 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
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:

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 ]

Method 3: Binary Search for Efficient Element Matching

In this approach, we use binary search to find the closest element in one of the arrays to a given target value from another array. This approach is effective because it leverages the fact that the arrays are sorted to quickly narrow down potential candidates.

Steps:

  1. Iterate through each element of the first array.
  2. For each element in the first array, use binary search to find the closest elements in the other two arrays.
  3. Calculate the difference between the maximum and minimum of the triplet formed by these elements.
  4. Track the minimum difference and update the triplet accordingly.

Example:

JavaScript
function binarySearch(arr, target) {
    let left = 0, right = arr.length - 1;
    while (left <= right) {
        let mid = Math.floor((left + right) / 2);
        if (arr[mid] === target) return mid;
        if (arr[mid] < target) left = mid + 1;
        else right = mid - 1;
    }
    return right;
}

function findSmallestDifferenceTriplet(arr1, arr2, arr3) {
    let minDifference = Number.MAX_SAFE_INTEGER;
    let resultTriplet = [];

    for (let i = 0; i < arr1.length; i++) {
        let a = arr1[i];

        // Find closest element in arr2
        let pos2 = binarySearch(arr2, a);
        let b1 = arr2[pos2] || Number.MAX_SAFE_INTEGER;
        let b2 = arr2[pos2 + 1] || Number.MAX_SAFE_INTEGER;
        let pos3 = binarySearch(arr3, a);
        let c1 = arr3[pos3] || Number.MAX_SAFE_INTEGER;
        let c2 = arr3[pos3 + 1] || Number.MAX_SAFE_INTEGER;
        for (let b of [b1, b2]) {
            for (let c of [c1, c2]) {
                let triplet = [a, b, c];
                let maxNum = Math.max(a, b, c);
                let minNum = Math.min(a, b, c);
                let difference = maxNum - minNum;

                if (difference < minDifference) {
                    minDifference = difference;
                    resultTriplet = triplet;
                }
            }
        }
    }

    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 ]


Next Article

Similar Reads