Kth Smallest Product of Two Sorted Arrays - Complete Solution Guide
Kth Smallest Product of Two Sorted Arrays is LeetCode problem 2040, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
Given two sorted 0-indexed integer arrays nums1 and nums2 as well as an integer k , return the k th ( 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 2 nd smallest product is 8. Example 2: Input: nums1 = [-4,-2,0,3], nums2 = [2,4], k = 6 Output: 0 Explanation: The
Detailed Explanation
The problem requires finding the k-th smallest product formed by multiplying elements from two sorted integer arrays, `nums1` and `nums2`. We need to consider all possible pairs of elements, one from each array, calculate their product, and then identify the k-th smallest among these products. Crucially, we don't need to actually calculate all the products and sort them; the goal is to find an efficient method to determine the k-th smallest product without explicitly generating all possible products.
Solution Approach
The solution uses binary search on the range of possible product values to find the k-th smallest product. The `count_le` function is a helper function that, given a value `val`, counts the number of products `nums1[i] * nums2[j]` that are less than or equal to `val`. The binary search iteratively narrows down the search space until it finds the smallest value `ans` such that there are at least `k` products less than or equal to `ans`.
Step-by-Step Algorithm
- Step 1: Initialize `low` and `high` for binary search. `low` is the smallest possible product (-10^10 - 1) and `high` is the largest possible product (10^10 + 1).
- Step 2: Implement the `count_le` function. This function takes a value `val` and counts the number of pairs `(nums1[i], nums2[j])` such that their product is less than or equal to `val`. It iterates through `nums1` and, for each element `n1`, counts how many elements `n2` in `nums2` satisfy the condition `n1 * n2 <= val`. Binary search is used within `count_le` to efficiently find this count for each `n1`.
- Step 3: Perform binary search on the range [low, high]. In each iteration, calculate the `mid` value and call `count_le(mid)`. If `count_le(mid)` is greater than or equal to `k`, it means that the k-th smallest product is less than or equal to `mid`. Update `ans` to `mid` and reduce the search space to `[low, mid - 1]`. Otherwise, increase the search space to `[mid + 1, high]`.
- Step 4: Return the final `ans` value, which represents the k-th smallest product.
Key Insights
- Insight 1: Binary Search is the key. The problem can be efficiently solved by using binary search on the range of possible product values. We don't need to generate all products.
- Insight 2: Counting products less than or equal to a given value. For a given value `mid`, efficiently determining the number of products less than or equal to `mid` is crucial for the binary search process.
- Insight 3: Handling negative and zero values. The arrays can contain negative, zero, and positive numbers. The count_le function must handle these cases correctly, as the order changes with the negative numbers.
- Insight 4: Potential Integer Overflow. The product of two integers from the input arrays might exceed the range of `int`. It is necessary to use `long` to store products to avoid overflow.
Complexity Analysis
Time Complexity: O(m * log(n) * log(10000000000))
Space Complexity: O(1)
Topics
This problem involves: Array, Binary Search.
Companies
Asked at: LinkedIn.