Alternative approach : forming a sort of cluster of values such that diff b/w max and min is minimized


#1

Following is my code:

int Solution::solve(vector &A, vector &B, vector &C) {
int i=0, j=0, k=0;
int n1=A.size(), n2=B.size(), n3=C.size();
int ans=INT_MAX;

while(i<n1 and j<n2 and k<n3){
    // finding element A[i] closest to B[j] 
    while(i<n1 and A[i]<B[j]) i++;
    if(i>0 and abs(A[i]-B[j]) >= abs(A[i-1]-B[j])) i--;

    // finding element C[k] closest to B[j]
    while(k<n3 and C[k]<B[j]) k++;
    if(k>0 and abs(C[k]-B[j]) >= abs(C[k-1]-B[j])) k--;
    
    // updating answer
    ans = min(ans, max(A[i], max(B[j], C[k])) - min(A[i], min(B[j], C[k])));
    
    // going to next anchor index j
    j++;
}
return ans;

}


#2

I think this can be improved upon by using binary search to find the approximate j and k.