Three nearby numbers in three arrays

前端 未结 2 774
旧时难觅i
旧时难觅i 2021-01-19 18:37

Given three sorted floating-point arrays a[], b[], and c[], design a linearithmic algorithm to find three integers i,

2条回答
  •  日久生厌
    2021-01-19 19:28

    The following algorithm is almost like merging three sorted arrays into one sorted one.

    Keep one pointer for each array (i,j,k for A, B and C respectively). Initialize them to 0.

    Then compute the difference between A[i], B[j], C[k] and update the lowest value achieved till now if necessary.

    Increment the index in the array for which

    array[index] =  min(A[i], B[j] and C[k]) 
    

    if it has not reached the end.

    That is:

    If ( A[i] is the least ), then increment i.
    else If ( B[j] is the least ), then increment j.
    else increment k.
    

    Keep doing the above till any one index runs past the end or you find a situation where all three A[i], B[j] and C[k] are equal.

    EDIT:
    If there are two duplicate candidates (say A[i] == B[j]), then increment both i and j. See for yourself why.

    Also, if A[i+1] == A[i], then simply increment i again.
    End Edit:

    The above algorithm has O(N) time complexity.

    Proof of correctness:
    As shown by other answer, the difference depends only on two extremes of A[i], B[j], C[k].

    So if A[i] < B[j] < C[k], then difference = 2*(C[k] - A[i]). Hence if we increment either j or k, then the difference can only increase. Hence we increment i.

提交回复
热议问题