Find the maximum difference between two given arrays such that the two numbers picked should not be from the same index

  • 0

    I recently faced this interview question and could not give a satisfactory answer. Wondering how to approach this question. The interviewer was looking for a linear time solution it seems. Basically, if we have two arrays for example:

    A = [10,9,8,7]
    B = [1,2,3,4]

    The maximum difference above would be (10-2)=8 and not (10-1)=9 because 10 and 1 are from the same index.

  • 2

    You can find the max number and the next biggest number max1, max2.
    Then find minimum number and the the smallest number min1, min2.
    if max1 and min1 have different index result is max1-min1.
    Otherwise result is max(max1-min2, max2-min1).
    O(N) time.

  • 1

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.