Consider 2 arrays of positive integers. We are given some predefined integer constant N. Now, we must find pairs, where 1 element brought from 1 array, and 2 - from second array, such that sum of integers equals N. If no such pairs found, return pairs, which sum is closest(but doesn't exceed) to N (it can be several pairs, if they have equal sum)
I have the only solution in my mind : Traverse all possible pairs, if we encounter pair of closer distance, clean resulting array and put it there. If we encounter the pair of exact distance, clear pairs with lower distance and put the pair into resulting array.
I believe there is more efficient way to solve the problem, do you have any ideas on that?