Problem Statement

You are given 3 arrays A, B and C. All 3 of the arrays are sorted.

Find i, j, k such that :max(abs(A[i] - B[j]), abs(B[j] - C[k]), abs(C[k] - A[i])) is minimized. Return the minimum max(abs(A[i] - B[j]), abs(B[j] - C[k]), abs(C[k] - A[i]))

Example :

`Input : A : [1, 4, 10] B : [2, 15, 20] C : [10, 12]

Output : 5 With 10 from A, 15 from B and 10 from C.`

Problem Link

Array 3 Pointers - InterviewBit

Reference Video

https://www.youtube.com/watch?v=3WGMZBq_jns

Code

int Solution::minimize(const vector<int> &A, const vector<int> &B, const vector<int> &C) {

    int i = 0, j = 0, k = 0;
    int res = INT_MAX;

    while(i<A.size()&&j<B.size()&&k<C.size())
    {
        res = min(res,max({abs(A[i] - B[j]), abs(B[j] - C[k]), abs(C[k] - A[i])}) );

        if(A[i]<=B[j]&&A[i]<=C[k])
        i++;

        else if(B[j]<=A[i]&&B[j]<=C[k])
        j++;

        else
        k++;
    }

    return res;
}