Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

Find list of closest pair from two unsorted array

I am trying to solve this problem for a while. For example, there two unsorted arrays:

A = [4,2,6] and B = [8,7,10] and target = 15

Now, I have to find the list of pairs' sum from A and B which are closest (less or equal) to the target. For this case the answers are [4, 10] and [6,8] as their sum is 14 which closest to 15.

I was able to do it in O(MN) and O(NlogN + MlogM + MlogN)

I am looking for a runtime O(N) or O(M) or O(M+N) and space of similar order. where M = the size of array A and N = the size of array B.

Comments