I have a question in which I am given a set of values SET A, and a set of values SET B. I am supposed to find the maximum number of pairs possible taking one value from set A and one value with set B. Condition- The difference between the two values should be less than 11.
eg- SET A-2,3,4 SET B-14,12,250 Max Pairs possible- (14,4) and (12,3) NOTE-(12,4) can also be a pair but then,it wont give us maximum possible sets, as 3 would be left. Therefor two achieve maximum 4 pairs up with 14 and 12 with 3.
I am able to solve this question in O(n^2) complexity, I was looking for a better solution.
I answered a similar question 10 minutes ago. The ides here is the same: loop over sorted ranges.
Here is the same code as in the other answer adapted to your problem (I just replaced the equality by a smaller-than relation):
Here is the application for your example,
With this one obtains the desired answer given in the OP:
DEMO.