Wednesday, July 10, 2024
HomeLanguagesJavascriptJavaScript Program to find Smallest Difference Triplet from Three Arrays

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

  • 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 ]

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

Shaida Kate Naidoo
am passionate about learning the latest technologies available to developers in either a Front End or Back End capacity. I enjoy creating applications that are well designed and responsive, in addition to being user friendly. I thrive in fast paced environments. With a diverse educational and work experience background, I excel at collaborating with teams both local and international. A versatile developer with interests in Software Development and Software Engineering. I consider myself to be adaptable and a self motivated learner. I am interested in new programming technologies, and continuous self improvement.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments