Grokking the Coding Interview: Patterns for Coding Questions
0% completed
Solution: Problem Challenge 1: K Pairs with Largest Sum
Problem Statement
Given two sorted arrays in descending order, find ‘K’ pairs with the largest sum where each pair consists of numbers from both the arrays.
Example 1:
Input: nums1=[9, 8, 2], nums2=[6, 3, 1], K=3
Output: [9, 3], [9, 6], [8, 6]
Explanation: These 3 pairs have the largest sum. No other pair has a sum larger than any of these.
Example 2:
Input: nums1=[5, 2, 1], nums2=[2, -1], K=3
Output: [5, 2], [5, -1], [2, 2]
Constraints:
- 1 <= nums1.length, nums2.length <= 10<sup>5</sup>
- -10<sup>9</sup> <= nums1[i], nums2[i] <= 10<sup>9</sup>
- `nums1
.....
.....
.....
Like the course? Get enrolled and start learning!
W
Will
· 4 years ago
There exists an O(KLogK) solution using a heap, but requires some non-obvious insight to come up with it. See Leetcode question 373.
Show 3 replies