You are given two integer arrays nums1
and nums2
sorted in non-decreasing order and an integer k
.
Define a pair (u, v)
which consists of one element from the first array and one element from the second array.
Return the k
pairs (u1, v1), (u2, v2), ..., (uk, vk)
with the smallest sums.
Example 1:
Input: nums1 = [1,7,11], nums2 = [2,4,6], k = 3
Output: [[1,2],[1,4],[1,6]]
Explanation: The first 3 pairs are returned from the sequence: [1,2],[1,4],[1,6],[7,2],[7,4],[11,2],[7,6],[11,4],[11,6]
Example 2:
Input: nums1 = [1,1,2], nums2 = [1,2,3], k = 2
Output: [[1,1],[1,1]]
Explanation: The first 2 pairs are returned from the sequence: [1,1],[1,1],[1,2],[2,1],[1,2],[2,2],[1,3],[1,3],[2,3]
Constraints:
1 <= nums1.length, nums2.length <= 10^5
-10^9 <= nums1[i], nums2[i] <= 10^9
nums1
and nums2
both are sorted in non-decreasing order.1 <= k <= 10^4
k <= nums1.length * nums2.length
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The goal is to find the pairs with the smallest sums from two lists of numbers. A brute force approach means we consider every possible pair we can make by picking one number from each list. Then, we calculate the sum for each of these pairs and find the K pairs with the smallest sums.
Here's how the algorithm would work step-by-step:
def find_k_pairs_with_smallest_sums_brute_force(list_one, list_two, k_smallest):
pair_sums = []
# Iterate through all possible pairs
for first_list_index in range(len(list_one)):
for second_list_index in range(len(list_two)):
# Calculate sum for the pair
current_sum = list_one[first_list_index] + list_two[second_list_index]
pair_sums.append((current_sum, (list_one[first_list_index], list_two[second_list_index])))
#Sort to find the K smallest sums
pair_sums.sort()
smallest_k_pairs = []
for i in range(min(k_smallest, len(pair_sums))):
# Extract pairs
smallest_k_pairs.append(pair_sums[i][1])
return smallest_k_pairs
The challenge involves finding the 'k' smallest sums formed by pairing numbers from two lists. Instead of checking all possible pairs, we'll focus on the pairs that are most likely to have small sums and efficiently find the top 'k'.
Here's how the algorithm would work step-by-step:
import heapq
def find_k_smallest_pairs(nums1, nums2, k):
result = []
visited = set()
heap = [(nums1[0] + nums2[0], 0, 0)]
visited.add((0, 0))
# We only need k smallest pairs
while heap and len(result) < k:
sum_of_pairs, index1, index2 = heapq.heappop(heap)
result.append((nums1[index1], nums2[index2]))
# Explore the neighbor to the right
if index1 + 1 < len(nums1) and (index1 + 1, index2) not in visited:
heapq.heappush(heap, (nums1[index1 + 1] + nums2[index2], index1 + 1, index2))
visited.add((index1 + 1, index2))
# Explore the neighbor below
if index2 + 1 < len(nums2) and (index1, index2 + 1) not in visited:
heapq.heappush(heap, (nums1[index1] + nums2[index2 + 1], index1, index2 + 1))
visited.add((index1, index2 + 1))
return result
Case | How to Handle |
---|---|
One or both input arrays are null or empty | Return an empty list if either input array is null or has zero length. |
k is zero or negative | If k is zero or negative, return an empty list, as no pairs are requested. |
k is larger than the total number of possible pairs (len(nums1) * len(nums2)) | Return all possible pairs up to the maximum number of pairs instead of crashing or causing an error. |
Input arrays contain large negative numbers | Handle potential integer overflow issues when calculating sums, perhaps using a larger data type. |
Input arrays contain duplicate numbers | The algorithm should correctly process duplicate numbers and consider all possible pair combinations, not skipping any because of duplicates. |
One array is significantly larger than the other (e.g., one has 10 elements, the other has 100,000) | The algorithm should still perform efficiently and not result in unnecessary iterations or memory usage by optimizing heap size. |
Arrays are sorted in descending order instead of ascending | The algorithm needs to work correctly whether input arrays are sorted ascending or descending because problem description does not specify sort order requirement. |
Input arrays contain very large numbers, potentially leading to integer overflow when summing | Utilize data types capable of handling large sums or implement checks to prevent integer overflow during the sum calculation. |