QUESTION
Find three closest elements from given three sorted arrays. Take the three sorted arrays and their sizes as input.
ANSWER
#include <iostream>
using namespace std;
#define INT_MAX 100
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];*/
int A[10],B[10],C[10],p,q,r,i;
cin>>p;
for(i=0;i<p;i++)
cin>>A[i];
cin>>q;
for(i=0;i<q;i++)
cin>>B[i];
cin>>r;
for(i=0;i<r;i++)
cin>>C[i];
findClosest(A, B, C, p, q, r);
return 0;
}