Three arrays of same size are given. Find a triplet such that maximum – minimum in that triplet is minimum of all the triplets. A triplet should be selected in a way such that it should have one number from each of the three given arrays.

If there are 2 or more smallest difference triplets, then the one with the smallest sum of its elements should be displayed.

Examples:

Input : arr1[] = {5, 2, 8} arr2[] = {10, 7, 12} arr3[] = {9, 14, 6} Output : 8, 7, 6 Input : arr1[] = {15, 12, 18, 9} arr2[] = {10, 17, 13, 8} arr3[] = {14, 16, 11, 5} Output : 11, 10, 9

**Note:**The elements of the triplet are displayed in non-decreasing order.

**Simple Solution : **Consider each an every triplet and find the required smallest difference triplet out of them. Complexity of O(n^{3}).

**Efficient Solution:**

- Sort the 3 arrays in non-decreasing order.
- Start three pointers from left most elements of three arrays.
- Now find min and max and calculate max-min from these three elements.
- Now increment pointer of minimum element’s array.
- Repeat steps 2, 3, 4, for the new set of pointers until any one pointer reaches to its end.

// C++ implementation of smallest difference triplet #include <bits/stdc++.h> using namespace std; // function to find maximum number int maximum(int a, int b, int c) { return max(max(a, b), c); } // function to find minimum number int minimum(int a, int b, int c) { return min(min(a, b), c); } // Finds and prints the smallest Difference Triplet void smallestDifferenceTriplet(int arr1[], int arr2[], int arr3[], int n) { // sorting all the three arrays sort(arr1, arr1+n); sort(arr2, arr2+n); sort(arr3, arr3+n); // To store resultant three numbers int res_min, res_max, res_mid; // pointers to arr1, arr2, arr3 // respectively int i = 0, j = 0, k = 0; // Loop until one array reaches to its end // Find the smallest difference. int diff = INT_MAX; while (i < n && j < n && k < n) { int sum = arr1[i] + arr2[j] + arr3[k]; // maximum number int max = maximum(arr1[i], arr2[j], arr3[k]); // Find minimum and increment its index. int min = minimum(arr1[i], arr2[j], arr3[k]); if (min == arr1[i]) i++; else if (min == arr2[j]) j++; else k++; // comparing new difference with the // previous one and updating accordingly if (diff > (max-min)) { diff = max - min; res_max = max; res_mid = sum - (max + min); res_min = min; } } // Print result cout << res_max << ", " << res_mid << ", " << res_min; } // Driver program to test above int main() { int arr1[] = {5, 2, 8}; int arr2[] = {10, 7, 12}; int arr3[] = {9, 14, 6}; int n = sizeof(arr1) / sizeof(arr1[0]); smallestDifferenceTriplet(arr1, arr2, arr3, n); return 0; }

Output:

7, 6, 5

Time Complexity: O(n log n)

This article is contributed by **Ayush Jauhari**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.