How to find pair with kth largest sum?

前端 未结 6 837
深忆病人
深忆病人 2020-12-02 12:29

Given two sorted arrays of numbers, we want to find the pair with the kth largest possible sum. (A pair is one element from the first array and one element from the second

6条回答
  •  [愿得一人]
    2020-12-02 13:01

    [2, 3, 5, 8, 13]
    [4, 8, 12, 16]
    

    Merge the 2 arrays and note down the indexes in the sorted array. Here is the index array looks like (starting from 1 not 0)

    [1, 2, 4, 6, 8] [3, 5, 7, 9]

    Now start from end and make tuples. sum the elements in the tuple and pick the kth largest sum.

提交回复
热议问题