Kth Smallest Product of Two Sorted Arrays in Java2 May 2025 | 4 min read Given two sorted integer arrays, nums1 and nums2, and an integer k. The task is to determine the kth (-based) least product of nums1[i] * nums2[j], where 0 <= i < nums1.length and 0 <= j < nums2.length. Example 1:Input: nums1 = [2,8], nums2 = [3,4,5], k = 4 Output: 24 Explanation: Four products that are less than or equal to 24 are possible: 2 * 3 = 6 2 * 4 = 8 2 * 5 = 10 8 * 3 = 24 Example 2:Input: nums1 = [-5,-3,0,3], nums2 = [2,4], k = 6 Output: 0 Explanation: There are 6 possible products that are less than or equal to 0: -5*2=-10 -5*4=-20 -3*2=-6 -3*4=-12 0*2=0 0*4=0 Example 3:Input: nums1 = [-6,-4,-3,0,1,3,4,7], nums2 = [-5,2,3,4], k = 40 Output: 147 Brute Force ApproachALGORITHM:Step 1: To store the products of components A and B, create an empty list called products. Step 2: Iterate over every element in A. Add a * b to products for every an as you iterate through each element b in B. Step 3: Use Collections. Sort to arrange the product list in ascending order. Step 4: Go to index k-1 in products to get the kth smallest product. Return products.get(k - 1) as the outcome. Implementation:The implementation of the above steps given below FileName: KthSmallestProduct.java Output: Kth Smallest Product is: 6 Complexity Analysis: Time Complexity: O(m⋅log (m⋅n)) is the time complexity. It takes O(m⋅n) to generate all products and O(m⋅log (m⋅n)) to sort them. Space Complexity: O(m⋅n) space is needed to store all m⋅n products in the products list. Optimal ApproachWe can utilize binary search on the potential product values to find the best answer. ALGORITHM: Step 1: First, set the leftmost product (A[0] * B[0]) to the left and the most prominent product (A[m-1] * B[n-1]) to the right. Here, m and n stand for the lengths of A and B, respectively. Step 2: As left is less than right in step two: Determine mid by taking the middle of left and right. To determine how many items are fewer than or equal to mid, use the helper function countLessThanOrEqual. Adjust left to mid + 1 if the count is less than k and right to mid otherwise. The kth most miniature products will be on the left after the loop ends. Step 3: For every element in A, count the products that are less than or equal to mid.
Give back the total number of an in A. Step 4: Finding the first location in B where elements are more significant than x. Binary Search for Bounds in B, upper bound. Lower-bound identifies the first place in B where at least x items are present. Implementation: The implementation of the above steps given below FileName: KthSmallestProduct1.java Output: Kth Smallest Product is: 6 Complexity Analysis: Time Complexity: O((m+n)log(max range)) It takes O(log(max range)) to perform a binary search on the product range and O(m+n) to count elements that are smaller than or equal to mid. Space Complexity: O(1) Only a fixed quantity of additional space is utilized. Next TopicNo Suitable Driver Found For JDBC |
FileInputStream finalize() Method in Java
The FileInputStream class of the Java programming language is used to read data from files in a byte-oriented fashion. It has several data reading methods, including read(), read(byte[]), and read(byte[], int, int). Finalise(), a method that the FileInputStream class inherits from the Object class, is one...
4 min read
Zipping and Unzipping Files in Java
ZIP is a common file format that compresses one or more files into a single location. It reduces the file size and makes it easier to transport or store. A recipient can unzip (or extract) a ZIP file after transport and use the file in the...
8 min read
Ad-hoc Polymorphism in Java
Java is a versatile and widely used programming language that provides a variety of features that support polymorphism. Polymorphism, a key concept in object-oriented design, allows us to write code that can work with different objects easily and conveniently Ad-hoc polymorphism in Java is an important...
4 min read
Difference Between Array and Vector in Java
Arrays and vectors are two commonly used programming structures when working with data sets. Although they are both used to store several pieces of the same sort, their features, performance, flexibility, and memory management are very different. What is an Array? A collection of elements can be...
6 min read
Difference Between Jdeps and Jdeprscan tools in Java
Difference Between Jdeps and Jdeprscan Tools in Java When it comes to developing and maintaining Java applications, tools that aid in dependency analysis and identifying deprecated APIs are invaluable. Two such tools provided by the Java platform are Jdeps and Jdeprscan. Despite their seemingly similar purposes, these...
3 min read
IsoChronology eras() method in Java with Examples
The java.time.chrono.IsoChronology class has the eras() method. All of the eras that fall under this specific Iso chronology are retrieved using the IsoChronology class. Syntax: public List eras() Parameter: No arguments are accepted as parameters for this method. Return Value: All of the eras that fall under...
2 min read
isnull() Method in Java
In the world of Java programming, dealing with null values is a common challenge. Handling nulls effectively is crucial to avoid NullPointerExceptions and ensure robust and error-free code. The isNull() method, available in various frameworks and libraries, is a powerful tool that allows developers to determine...
4 min read
Java ByteBuffer Size
A Java 'ByteBuffer' is a container for a fixed number of bytes. The size of a 'ByteBuffer' is the number of bytes it can hold, and it is determined when the 'ByteBuffer' is created. We can create a 'ByteBuffer' with a specific size in bytes using...
5 min read
How to Enable Java in Chrome
The Java plugin is part of the Java Runtime Environment (JRE). It allows a browser to work with the Java platform to run Java applets. Almost all the browsers enable Java plugin but sometimes we get the error like the Chrome does not support Java. To...
3 min read
Rotate Matrix by 90 Degrees in Java
| Rotate Matrix in Java Clockwise and Anti-clockwise In this section, we will create a Java program to rotate a matrix by 90 degrees in a clockwise and anti-clockwise rotation. The clockwise rotation is also known as the right rotation of the matrix and the anti-clockwise...
3 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India