Problem 350104 · medium · Phase 03 Linear Management & Searching

Kth Smallest in a Times Table

binary search on the answer · counting

An m x n multiplication table has the value i * j in row i, column j (both 1-indexed). Return the k-th smallest value in the table, counting duplicates separately.

Examples

Input:  m = 3, n = 3, k = 5
Output: 3
Explanation: the entries in order are 1, 2, 2, 3, 3, 4, 6, 6, 9.

Input:  m = 2, n = 3, k = 6
Output: 6

Input:  m = 1, n = 1, k = 1
Output: 1

Constraints

  • 1 <= m, n <= 3 * 10**4, 1 <= k <= m * n
  • Building the whole table is far too slow; aim for O(min(m, n) * log(m * n)).

Goals

  • Count how many table entries are <= v in O(m) time
  • Binary search on the value rather than materialising the table
Starting Python…