Problem 350104 · medium · Level 03 Linear Management & Searching

Rug Areas in Order

binary search on the answer · counting

A rug maker sells rectangular rugs in every whole-metre size from 1 to m metres wide and 1 to n metres long, so the i by j rug covers i * j square metres (a times table of areas). Line up the areas of all m * n sizes from smallest to largest, keeping equal areas as separate entries, and return the k-th.

Examples

Input:  m = 4, n = 3, k = 7
Output: 4
Explanation: the 12 areas in order are 1, 2, 2, 3, 3, 4, 4, 6, 6, 8, 9, 12.

Input:  m = 2, n = 4, k = 8
Output: 8

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…