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

前端 未结 3 376
时光取名叫无心
时光取名叫无心 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 20:15

    Sort arrays A & B : O(mlogm + nlogn) Apply a modified form of algorithm for merging 2 sorted arrays : O(m+n) i.e. at each point, u sum the the two elements. When u have got (m+n-k+1)th element in C, stop merging. That element is essentially kth largest. E.g. {1,2} & {3,4} : Sorted C: {1+3,(1+4)|(2+3),2+4}

提交回复
热议问题