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 : 7, 6, 5
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(n3).
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.
Implementatipon:
C++
#include <bits/stdc++.h>
using namespace std;
int maximum( int a, int b, int c)
{
return max(max(a, b), c);
}
int minimum( int a, int b, int c)
{
return min(min(a, b), c);
}
void smallestDifferenceTriplet( int arr1[], int arr2[],
int arr3[], int n)
{
sort(arr1, arr1+n);
sort(arr2, arr2+n);
sort(arr3, arr3+n);
int res_min, res_max, res_mid;
int i = 0, j = 0, k = 0;
int diff = INT_MAX;
while (i < n && j < n && k < n)
{
int sum = arr1[i] + arr2[j] + arr3[k];
int max = maximum(arr1[i], arr2[j], arr3[k]);
int min = minimum(arr1[i], arr2[j], arr3[k]);
if (min == arr1[i])
i++;
else if (min == arr2[j])
j++;
else
k++;
if (diff > (max-min))
{
diff = max - min;
res_max = max;
res_mid = sum - (max + min);
res_min = min;
}
}
cout << res_max << ", " << res_mid << ", " << res_min;
}
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;
}
|
Java
import java.util.Arrays;
class GFG {
static int maximum( int a, int b, int c)
{
return Math.max(Math.max(a, b), c);
}
static int minimum( int a, int b, int c)
{
return Math.min(Math.min(a, b), c);
}
static void smallestDifferenceTriplet( int arr1[],
int arr2[], int arr3[], int n)
{
Arrays.sort(arr1);
Arrays.sort(arr2);
Arrays.sort(arr3);
int res_min= 0 , res_max= 0 , res_mid= 0 ;
int i = 0 , j = 0 , k = 0 ;
int diff = 2147483647 ;
while (i < n && j < n && k < n)
{
int sum = arr1[i] + arr2[j] + arr3[k];
int max = maximum(arr1[i], arr2[j], arr3[k]);
int min = minimum(arr1[i], arr2[j], arr3[k]);
if (min == arr1[i])
i++;
else if (min == arr2[j])
j++;
else
k++;
if (diff > (max - min))
{
diff = max - min;
res_max = max;
res_mid = sum - (max + min);
res_min = min;
}
}
System.out.print(res_max + ", " + res_mid
+ ", " + res_min);
}
public static void main (String[] args)
{
int arr1[] = { 5 , 2 , 8 };
int arr2[] = { 10 , 7 , 12 };
int arr3[] = { 9 , 14 , 6 };
int n = arr1.length;
smallestDifferenceTriplet(arr1, arr2, arr3, n);
}
}
|
Python3
def maximum(a, b, c):
return max ( max (a, b), c)
def minimum(a, b, c):
return min ( min (a, b), c)
def smallestDifferenceTriplet(arr1, arr2, arr3, n):
arr1.sort()
arr2.sort()
arr3.sort()
res_min = 0 ; res_max = 0 ; res_mid = 0
i = 0 ; j = 0 ; k = 0
diff = 2147483647
while (i < n and j < n and k < n):
sum = arr1[i] + arr2[j] + arr3[k]
max = maximum(arr1[i], arr2[j], arr3[k])
min = minimum(arr1[i], arr2[j], arr3[k])
if ( min = = arr1[i]):
i + = 1
else if ( min = = arr2[j]):
j + = 1
else :
k + = 1
if (diff > ( max - min )):
diff = max - min
res_max = max
res_mid = sum - ( max + min )
res_min = min
print (res_max, "," , res_mid, "," , res_min)
arr1 = [ 5 , 2 , 8 ]
arr2 = [ 10 , 7 , 12 ]
arr3 = [ 9 , 14 , 6 ]
n = len (arr1)
smallestDifferenceTriplet(arr1, arr2, arr3, n)
|
C#
using System;
class GFG
{
static int maximum( int a, int b, int c)
{
return Math.Max(Math.Max(a, b), c);
}
static int minimum( int a, int b, int c)
{
return Math.Min(Math.Min(a, b), c);
}
static void smallestDifferenceTriplet( int []arr1,
int []arr2,
int []arr3,
int n)
{
Array.Sort(arr1);
Array.Sort(arr2);
Array.Sort(arr3);
int res_min = 0, res_max = 0, res_mid = 0;
int i = 0, j = 0, k = 0;
int diff = 2147483647;
while (i < n && j < n && k < n)
{
int sum = arr1[i] +
arr2[j] + arr3[k];
int max = maximum(arr1[i],
arr2[j], arr3[k]);
int min = minimum(arr1[i],
arr2[j], arr3[k]);
if (min == arr1[i])
i++;
else if (min == arr2[j])
j++;
else
k++;
if (diff > (max - min))
{
diff = max - min;
res_max = max;
res_mid = sum - (max + min);
res_min = min;
}
}
Console.WriteLine(res_max + ", " +
res_mid + ", " +
res_min);
}
static public void Main ()
{
int []arr1 = {5, 2, 8};
int []arr2 = {10, 7, 12};
int []arr3 = {9, 14, 6};
int n = arr1.Length;
smallestDifferenceTriplet(arr1, arr2,
arr3, n);
}
}
|
PHP
<?php
function maximum( $a , $b , $c )
{
return max(max( $a , $b ), $c );
}
function minimum( $a , $b , $c )
{
return min(min( $a , $b ), $c );
}
function smallestDifferenceTriplet( $arr1 , $arr2 ,
$arr3 , $n )
{
sort( $arr1 );
sort( $arr2 );
sort( $arr3 );
$res_min ; $res_max ; $res_mid ;
$i = 0; $j = 0; $k = 0;
$diff = PHP_INT_MAX;
while ( $i < $n && $j < $n && $k < $n )
{
$sum = $arr1 [ $i ] +
$arr2 [ $j ] +
$arr3 [ $k ];
$max = maximum( $arr1 [ $i ],
$arr2 [ $j ],
$arr3 [ $k ]);
$min = minimum( $arr1 [ $i ],
$arr2 [ $j ],
$arr3 [ $k ]);
if ( $min == $arr1 [ $i ])
$i ++;
else if ( $min == $arr2 [ $j ])
$j ++;
else
$k ++;
if ( $diff > ( $max - $min ))
{
$diff = $max - $min ;
$res_max = $max ;
$res_mid = $sum - ( $max + $min );
$res_min = $min ;
}
}
echo $res_max , ", " ,
$res_mid , ", " ,
$res_min ;
}
$arr1 = array (5, 2, 8);
$arr2 = array (10, 7, 12);
$arr3 = array (9, 14, 6);
$n = sizeof( $arr1 );
smallestDifferenceTriplet( $arr1 , $arr2 ,
$arr3 , $n );
?>
|
Javascript
<script>
function maximum(a, b, c)
{
return Math.max(Math.max(a, b), c);
}
function minimum(a, b, c)
{
return Math.min(Math.min(a, b), c);
}
function smallestDifferenceTriplet(arr1, arr2, arr3, n)
{
arr1.sort( function (a, b){ return a - b});
arr2.sort( function (a, b){ return a - b});
arr3.sort( function (a, b){ return a - b});
let res_min = 0, res_max = 0, res_mid = 0;
let i = 0, j = 0, k = 0;
let diff = 2147483647;
while (i < n && j < n && k < n)
{
let sum = arr1[i] + arr2[j] + arr3[k];
let max = maximum(arr1[i], arr2[j], arr3[k]);
let min = minimum(arr1[i], arr2[j], arr3[k]);
if (min == arr1[i])
i++;
else if (min == arr2[j])
j++;
else
k++;
if (diff > (max - min))
{
diff = max - min;
res_max = max;
res_mid = sum - (max + min);
res_min = min;
}
}
document.write(res_max + ", " + res_mid + ", " + res_min);
}
let arr1 = [5, 2, 8];
let arr2 = [10, 7, 12];
let arr3 = [9, 14, 6];
let n = arr1.length;
smallestDifferenceTriplet(arr1, arr2, arr3, n);
</script>
|
Time Complexity : O(n log n)
Auxiliary Space: O(1), since no extra space has been taken.
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
21 Jul, 2022
Like Article
Save Article