21. Kth Smallest Number in Multiplication Table
The problem can be found at the following link: ๐ Question Link
๐งฉ Problem Description
Given three integers m
, n
, and k
, representing an m ร n
multiplication table (1-indexed), return the k
th smallest element from it. Each cell of the matrix is defined as: mat[i][j] = i ร j
The multiplication table is filled in row-wise sorted and column-wise sorted order.
๐ Examples
Example 1:
Input: m = 3
, n = 3
, k = 5
Output: 3
Explanation:
The elements in increasing order are: [1, 2, 2, 3, 3, 4, 6, 6, 9]
โ the 5th smallest is 3
.
Example 2:
Input: m = 2
, n = 3
, k = 6
Output: 6
Explanation: The multiplication table is:
[1, 2, 3]
[2, 4, 6]
Flattened and sorted: [1, 2, 2, 3, 4, 6]
โ the 6th smallest is 6
.
๐ Constraints
$1 \leq m, n \leq 3 \times 10^4$
$1 \leq k \leq m \times n$
โ
My Approach
Binary Search on Value
We are asked to find the k
th smallest value in a virtual m ร n
multiplication table without building the table. Since values range from 1
to m ร n
, and the table is sorted row-wise and column-wise, we can binary search over the value space, not the index space.
At each step of binary search:
We guess a value
mid
, and count how many numbers in the table areโค mid
.For each row
i
(1-based), the count of elements โคmid
ismin(n, mid // i)
.If total count โฅ
k
, thenmid
might be the answer (go left). Else, go right.
Algorithm Steps:
Initialize the binary search bounds:
l = 1
,r = m * n
.While
l < r
:Compute
mid = (l + r) // 2
.Count how many elements in the table are
โค mid
.If count <
k
, setl = mid + 1
Else, setr = mid
.
Return
l
.
๐งฎ Time and Auxiliary Space Complexity
Expected Time Complexity: O(log(m ร n) ร min(m, n)), as we binary search over value space and count in each row up to
min(m, n)
.Expected Auxiliary Space Complexity: O(1), as we use only a constant amount of extra space.
๐ง Code (C++)
class Solution {
public:
int kthSmallest(int m, int n, int k) {
int l = 1, r = m * n;
while (l < r) {
int mid = (l + r) / 2, cnt = 0;
for (int i = 1; i <= m; ++i) cnt += min(n, mid / i);
cnt < k ? l = mid + 1 : r = mid;
}
return l;
}
};
๐งโ๐ป Code (Java)
class Solution {
public int kthSmallest(int m, int n, int k) {
int l = 1, r = m * n;
while (l < r) {
int mid = (l + r) / 2, cnt = 0;
for (int i = 1; i <= m; i++) cnt += Math.min(n, mid / i);
if (cnt < k) l = mid + 1;
else r = mid;
}
return l;
}
}
๐ Code (Python)
class Solution(object):
def kthSmallest(self, m, n, k):
l, r = 1, m * n
while l < r:
mid = (l + r) // 2
cnt = sum(min(n, mid // i) for i in range(1, m + 1))
if cnt < k: l = mid + 1
else: r = mid
return l
๐ง Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: ๐ฌ Any Questions?. Letโs make this learning journey more collaborative!
โญ If you find this helpful, please give this repository a star! โญ
๐Visitor Count
Last updated