Kth Smallest Number in Multiplication Table - Problem

Nearly everyone has used the Multiplication Table. The multiplication table of size m x n is an integer matrix mat where mat[i][j] == i * j (1-indexed).

Given three integers m, n, and k, return the k-th smallest element in the m x n multiplication table.

Example:

  • For m = 3, n = 3, k = 5, the multiplication table is:
1  2  3
2  4  6
3  6  9

The sorted elements are: [1, 2, 2, 3, 3, 4, 6, 6, 9], so the 5th smallest is 3.

Input & Output

Example 1 — Basic Case
$ Input: m = 3, n = 3, k = 5
Output: 3
💡 Note: The 3×3 multiplication table is [[1,2,3],[2,4,6],[3,6,9]]. Sorted values: [1,2,2,3,3,4,6,6,9]. The 5th smallest is 3.
Example 2 — First Element
$ Input: m = 2, n = 3, k = 1
Output: 1
💡 Note: The 2×3 table is [[1,2,3],[2,4,6]]. Sorted: [1,2,2,3,4,6]. The 1st smallest is 1.
Example 3 — Last Element
$ Input: m = 2, n = 2, k = 4
Output: 4
💡 Note: The 2×2 table is [[1,2],[2,4]]. Sorted: [1,2,2,4]. The 4th smallest is 4.

Constraints

  • 1 ≤ m, n ≤ 3 * 104
  • 1 ≤ k ≤ m * n

Visualization

Tap to expand
Kth Smallest in Multiplication Table3×3 Multiplication Table123246369Sorted Values122334669k = 5, so we want the 5th elementAnswer: 3Binary search avoids generating all O(m×n) values
Understanding the Visualization
1
Input
m×n multiplication table with values i×j
2
Process
Find k-th smallest without generating all values
3
Output
Return the k-th smallest number
Key Takeaway
🎯 Key Insight: Binary search on the answer space and count elements efficiently instead of generating all values
Asked in
Google 25 Amazon 20 Facebook 15
28.0K Views
Medium Frequency
~25 min Avg. Time
856 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen