Kth Smallest Product of Two Sorted Arrays
HARDDescription
nums1 and nums2 as well as an integer k, return the kth (1-based) smallest product of nums1[i] * nums2[j] where 0 <= i < nums1.length and 0 <= j < nums2.length.
Example 1:
Input: nums1 = [2,5], nums2 = [3,4], k = 2 Output: 8 Explanation: The 2 smallest products are: - nums1[0] * nums2[0] = 2 * 3 = 6 - nums1[0] * nums2[1] = 2 * 4 = 8 The 2nd smallest product is 8.
Example 2:
Input: nums1 = [-4,-2,0,3], nums2 = [2,4], k = 6 Output: 0 Explanation: The 6 smallest products are: - nums1[0] * nums2[1] = (-4) * 4 = -16 - nums1[0] * nums2[0] = (-4) * 2 = -8 - nums1[1] * nums2[1] = (-2) * 4 = -8 - nums1[1] * nums2[0] = (-2) * 2 = -4 - nums1[2] * nums2[0] = 0 * 2 = 0 - nums1[2] * nums2[1] = 0 * 4 = 0 The 6th smallest product is 0.
Example 3:
Input: nums1 = [-2,-1,0,1,2], nums2 = [-3,-1,2,4,5], k = 3 Output: -6 Explanation: The 3 smallest products are: - nums1[0] * nums2[4] = (-2) * 5 = -10 - nums1[0] * nums2[3] = (-2) * 4 = -8 - nums1[4] * nums2[0] = 2 * (-3) = -6 The 3rd smallest product is -6.
Constraints:
1 <= nums1.length, nums2.length <= 5 * 104-105 <= nums1[i], nums2[j] <= 1051 <= k <= nums1.length * nums2.lengthnums1andnums2are sorted.
Approaches
Checkout 3 different approaches to solve Kth Smallest Product of Two Sorted Arrays. Click on different approaches to view the approach and algorithm in detail.
Brute Force: Generate and Sort
The most straightforward approach is to generate all possible products, store them in a list, sort the list, and then pick the k-th element. This method is simple to understand and implement but is highly inefficient for the given constraints.
Algorithm
- Initialize an empty list,
products. - Iterate through each element
num1in thenums1array. - For each
num1, iterate through each elementnum2in thenums2array. - Calculate the product
p = (long)num1 * num2. - Add the product
pto theproductslist. - After iterating through all pairs, sort the
productslist in non-decreasing order. - The k-th smallest product is the element at index
k-1in the sorted list. Returnproducts.get(k-1).
This method involves a nested loop to compute every possible product of pairs (nums1[i], nums2[j]). All these m * n products are stored in a dynamic array or list. Once all products are generated, the list is sorted numerically. The k-th smallest product is then simply the element at the (k-1)-th index of this sorted list (since k is 1-based).
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
class Solution {
public long kthSmallestProduct(int[] nums1, int[] nums2, long k) {
List<Long> products = new ArrayList<>();
for (int num1 : nums1) {
for (int num2 : nums2) {
products.add((long) num1 * num2);
}
}
Collections.sort(products);
return products.get((int) k - 1);
}
}
Complexity Analysis
Pros and Cons
- Simple to understand and implement.
- Guaranteed to be correct.
- Exceeds time limits for large inputs due to
O(m*n*log(m*n))complexity. - Exceeds memory limits for large inputs as it requires storing
m*nproducts.
Code Solutions
Checking out 3 solutions in different languages for Kth Smallest Product of Two Sorted Arrays. Click on different languages to view the code.
Video Solution
Watch the video walkthrough for Kth Smallest Product of Two Sorted Arrays
Similar Questions
5 related questions you might find useful
Algorithms:
Data Structures:
Companies:
Subscribe to Scale Engineer newsletter
Learn about System Design, Software Engineering, and interview experiences every week.
No spam, unsubscribe at any time.