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.
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).