给定三个排序的数组A [],B []和C [],分别从A,B和C中找到3个元素i,j和k,这样max(abs(A [i] – B [j]),abs( B [j] – C [k]),abs(C [k] – A [i]))最小。这里abs()表示绝对值。
例子 :
Input: A[] = {1, 4, 10}
B[] = {2, 15, 20}
C[] = {10, 12}
Output: 10 15 10
10 from A, 15 from B and 10 from C
Input: A[] = {20, 24, 100}
B[] = {2, 19, 22, 79, 800}
C[] = {10, 12, 23, 24, 119}
Output: 24 22 23
24 from A, 22 from B and 23 from C
强烈建议您最小化浏览器,然后自己尝试。
一个简单的解决方案是运行三个嵌套循环以考虑A,B和C中的所有三元组。计算max(abs(A [i] – B [j]),abs(B [j] – C [k]]的值),每个三元组为abs(C [k] – A [i])),并返回所有值的最小值。该解决方案的时间复杂度为O(n 3 )
更好的解决方案是对我们进行二进制搜索。
1)遍历A []的所有元素,
a)对元素小于或等于B []和C []中的元素进行二进制搜索,并注意区别。
2)对B []和C []重复步骤1。
3)返回整体最小值。
该解决方案的时间复杂度为O(nLogn)
有效的解决方案让’p’为A []的大小,’q’为B []的大小,’r’为C []的大小
1) Start with i=0, j=0 and k=0 (Three index variables for A,
B and C respectively)
// p, q and r are sizes of A[], B[] and C[] respectively.
2) Do following while i < p and j < q and k < r
a) Find min and maximum of A[i], B[j] and C[k]
b) Compute diff = max(X, Y, Z) - min(A[i], B[j], C[k]).
c) If new result is less than current result, change
it to the new result.
d) Increment the pointer of the array which contains
the minimum.
请注意,我们增加了具有最小值的数组的指针,因为我们的目标是减小差异。增加最大指针会增加差异。增加第二个最大指针可能会增加差异。
C++
// C++ program to find 3 elements such that max(abs(A[i]-B[j]), abs(B[j]-
// C[k]), abs(C[k]-A[i])) is minimized.
#include
using namespace std;
void findClosest(int A[], int B[], int C[], int p, int q, int r)
{
int diff = INT_MAX; // Initialize min diff
// Initialize result
int res_i =0, res_j = 0, res_k = 0;
// Traverse arrays
int i=0,j=0,k=0;
while (i < p && j < q && k < r)
{
// Find minimum and maximum of current three elements
int minimum = min(A[i], min(B[j], C[k]));
int maximum = max(A[i], max(B[j], C[k]));
// Update result if current diff is less than the min
// diff so far
if (maximum-minimum < diff)
{
res_i = i, res_j = j, res_k = k;
diff = maximum - minimum;
}
// We can't get less than 0 as values are absolute
if (diff == 0) break;
// Increment index of array with smallest value
if (A[i] == minimum) i++;
else if (B[j] == minimum) j++;
else k++;
}
// Print result
cout << A[res_i] << " " << B[res_j] << " " << C[res_k];
}
// Driver program
int main()
{
int A[] = {1, 4, 10};
int B[] = {2, 15, 20};
int C[] = {10, 12};
int p = sizeof A / sizeof A[0];
int q = sizeof B / sizeof B[0];
int r = sizeof C / sizeof C[0];
findClosest(A, B, C, p, q, r);
return 0;
}
Java
// Java program to find 3 elements such
// that max(abs(A[i]-B[j]), abs(B[j]-C[k]),
// abs(C[k]-A[i])) is minimized.
import java.io.*;
class GFG {
static void findClosest(int A[], int B[], int C[],
int p, int q, int r)
{
int diff = Integer.MAX_VALUE; // Initialize min diff
// Initialize result
int res_i =0, res_j = 0, res_k = 0;
// Traverse arrays
int i = 0, j = 0, k = 0;
while (i < p && j < q && k < r)
{
// Find minimum and maximum of current three elements
int minimum = Math.min(A[i],
Math.min(B[j], C[k]));
int maximum = Math.max(A[i],
Math.max(B[j], C[k]));
// Update result if current diff is
// less than the min diff so far
if (maximum-minimum < diff)
{
res_i = i;
res_j = j;
res_k = k;
diff = maximum - minimum;
}
// We can't get less than 0
// as values are absolute
if (diff == 0) break;
// Increment index of array
// with smallest value
if (A[i] == minimum) i++;
else if (B[j] == minimum) j++;
else k++;
}
// Print result
System.out.println(A[res_i] + " " +
B[res_j] + " " + C[res_k]);
}
// Driver code
public static void main (String[] args)
{
int A[] = {1, 4, 10};
int B[] = {2, 15, 20};
int C[] = {10, 12};
int p = A.length;
int q = B.length;
int r = C.length;
// Function calling
findClosest(A, B, C, p, q, r);
}
}
// This code is contributed by Ajit.
Python3
# Python program to find 3 elements such
# that max(abs(A[i]-B[j]), abs(B[j]- C[k]),
# abs(C[k]-A[i])) is minimized.
import sys
def findCloset(A, B, C, p, q, r):
# Initialize min diff
diff = sys.maxsize
res_i = 0
res_j = 0
res_k = 0
# Travesre Array
i = 0
j = 0
k = 0
while(i < p and j < q and k < r):
# Find minimum and maximum of
# current three elements
minimum = min(A[i], min(B[j], C[k]))
maximum = max(A[i], max(B[j], C[k]));
# Update result if current diff is
# less than the min diff so far
if maximum-minimum < diff:
res_i = i
res_j = j
res_k = k
diff = maximum - minimum;
# We can 't get less than 0 as
# values are absolute
if diff == 0:
break
# Increment index of array with
# smallest value
if A[i] == minimum:
i = i+1
elif B[j] == minimum:
j = j+1
else:
k = k+1
# Print result
print(A[res_i], " ", B[res_j], " ", C[res_k])
# Driver Program
A = [1, 4, 10]
B = [2, 15, 20]
C = [10, 12]
p = len(A)
q = len(B)
r = len(C)
findCloset(A,B,C,p,q,r)
# This code is contributed by Shrikant13.
C#
// C# program to find 3 elements
// such that max(abs(A[i]-B[j]),
// abs(B[j]-C[k]), abs(C[k]-A[i]))
// is minimized.
using System;
class GFG
{
static void findClosest(int []A, int []B,
int []C, int p,
int q, int r)
{
// Initialize min diff
int diff = int.MaxValue;
// Initialize result
int res_i = 0,
res_j = 0,
res_k = 0;
// Traverse arrays
int i = 0, j = 0, k = 0;
while (i < p && j < q && k < r)
{
// Find minimum and maximum
// of current three elements
int minimum = Math.Min(A[i],
Math.Min(B[j], C[k]));
int maximum = Math.Max(A[i],
Math.Max(B[j], C[k]));
// Update result if current
// diff is less than the min
// diff so far
if (maximum - minimum < diff)
{
res_i = i;
res_j = j;
res_k = k;
diff = maximum - minimum;
}
// We can't get less than 0
// as values are absolute
if (diff == 0) break;
// Increment index of array
// with smallest value
if (A[i] == minimum) i++;
else if (B[j] == minimum) j++;
else k++;
}
// Print result
Console.WriteLine(A[res_i] + " " +
B[res_j] + " " +
C[res_k]);
}
// Driver code
public static void Main ()
{
int []A = {1, 4, 10};
int []B = {2, 15, 20};
int []C = {10, 12};
int p = A.Length;
int q = B.Length;
int r = C.Length;
// Function calling
findClosest(A, B, C, p, q, r);
}
}
// This code is contributed
// by anuj_67.
PHP
Javascript
输出:
10 15 10