Java | Binary Search Solution
2040. Kth Smallest Product of Two Sorted Arrays\n\nPlease \uD83D\uDD3C upvote this post if you find the answer useful & do comment about your thoughts \uD83D\uD
# [2040. Kth Smallest Product of Two Sorted Arrays](https://leetcode.com/problems/kth-smallest-product-of-two-sorted-arrays/)\n\n**Please** \uD83D\uDD3C **upvote this post if you find the answer useful & do comment about your thoughts** \uD83D\uDCAC\n\n## Java | Binary Search Solution\n\n### Java Code\n\n```\nclass Solution {\n    public long kthSmallestProduct(int[] nums1, int[] nums2, long k) {\n        return kthSmallest(nums1, nums2, k);\n    }\n\n\tpublic static long kthSmallest(int[] nums1, int[] nums2, long k) {\n\n\t\tlong lo = -1000_000_0000l;\n\t\tlong hi = 1000_000_0000l;\n\t\tlong ans = 0;\n\n\t\t// Binary Search\n\t\twhile (lo <= hi) {\n\t\t\tlong mid = (lo + hi) / 2;\n\t\t\tif (countNumberOfElements(nums1, nums2, mid) >= k) {\n\t\t\t\tans = mid;\n\t\t\t\thi = mid - 1;\n\t\t\t} else {\n\t\t\t\tlo = mid + 1;\n\t\t\t}\n\t\t}\n\t\treturn ans;\n\n\t}\n\n\tpublic static long countNumberOfElements(int[] arr1, int[] arr2, long Dot_pot) {\n\t\tlong ans = 0;\n\t\tfor (int e1 : arr1) {\n\t\t\tint count = 0;\n\t\t\tif (e1 >= 0) {\n\t\t\t\tint lo = 0;\n\t\t\t\tint hi = arr2.length - 1;\n\t\t\t\twhile (lo <= hi) {\n\t\t\t\t\tint mid = (lo + hi) / 2;\n\t\t\t\t\tif ((long) e1 * arr2[mid] <= Dot_pot) {\n\t\t\t\t\t\tcount = mid + 1;\n\t\t\t\t\t\tlo = mid + 1;\n\t\t\t\t\t} else {\n\t\t\t\t\t\thi = mid - 1;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tans = ans + count;\n\n\t\t\t}\n\n\t\t\telse {\n\n\t\t\t\tint lo = 0;\n\t\t\t\tint hi = arr2.length - 1;\n\t\t\t\twhile (lo <= hi) {\n\t\t\t\t\tint mid = (lo + hi) / 2;\n\t\t\t\t\tif ((long) e1 * arr2[mid] <= Dot_pot) {\n\t\t\t\t\t\tcount = arr2.length - mid;\n\t\t\t\t\t\thi = mid - 1;\n\n\t\t\t\t\t} else {\n\t\t\t\t\t\tlo = mid + 1;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tans = ans + count;\n\n\t\t\t}\n\n\t\t}\n\t\treturn ans;\n\t}\n\n}\n```
['Binary Tree', 'Java']