How to find kth largest number in pairwise sums like setA + setB?

前端 未结 3 384
时光取名叫无心
时光取名叫无心 2021-01-12 19:32

Here are two integers set, say A and B and we can get another set C, in which every element is sum of element a in A and element b in B.

For example, A = {1,2}, B =

3条回答
  •  再見小時候
    2021-01-12 19:52

    Well, O(n) would be a lower bound (probably not tight though), otherwise you could run the O(n) algorithm n times to get a sorted list in O(n^2).

    Can you assume the two sets are sorted (you present them in sorted order above)? If so, you could possibly get something with an average case that's decently better by doing an "early out", starting at the last pair of elements, etc. Just a hunch though.

提交回复
热议问题