Take three pointers p1, p2 and p3 respectively to the arrays A[]

**,**B[] and C[].1. Place all of them at 0th index and minDistance = some very large number

2. Now, find the max and min of the elements pointed to by the pointers.

3. Calculate dist = max - min. if dist is less than minDistance then Store p1,p2 and p3 in index1, index2, index3 and store dist in minDistance. Now, our job is to minimize this dist.

4. Its obvious that dist will be minimized only if we increment the pointer at the min element. Hence, increment the min pointer and repeat step 2.

4. Its obvious that dist will be minimized only if we increment the pointer at the min element. Hence, increment the min pointer and repeat step 2.

Implementation of above logic is as follows --

int max(int i, int j, int k)

{

return (i > j) ? ( i > k ? i : k) : (j > k ? j : k);

}

int min(int i, int j, int k, int& arr)

{

int temp = (i < j) ? ( i < k ? i : k) : (j < k ? j : k);

arr = (temp == i) ? 0 : (temp == j ? 1 : 2);

return temp;

}

void FindTriplet(int *a, int *b, int *c, int lenA, int lenB, int lenC)

{

int index[3] = { 0 };

int minArr = 0;

int minDistance = 32767; // Large number

int minIndex[3] = {0};

while(index[0] < lenA && index[1] < lenB && index[2] < lenC)

{

int distance = max(a[index[0]], b[index[1]], c[index[2]]) -

min(a[index[0]], b[index[1]], c[index[2]], minArr);

if(distance < minDistance)

{

for(int i =0; i<3; minIndex[i++] = index[i]);

if(0 == (minDistance = distance) )

break;

}

index[minArr]++ ;

}

cout<<"Minimum Distance :: "<<minDistance<<"\nAt indices\t"<<minIndex[0]<<'\t'

<<minIndex[1]<<'\t'<<minIndex[2]<<'\n';

}