Help Needed. Have Been trying the proof of this problem's solution for a long time

Revision en1, by 51stDimension, 2021-08-07 14:39:00

Quesion Link Leetcode: Leetode Question Link Codeforces Link

This problem has a binary search solution which goes like this:

class Solution {
    public boolean enough(int x, int m, int n, int k) {
        int count = 0;
        for (int i = 1; i <= m; i++) {
            count += Math.min(x / i, n);
        }
        return count >= k;
    }

    public int findKthNumber(int m, int n, int k) {
        int lo = 1, hi = m * n;
        while (lo < hi) {
            int mi = lo + (hi - lo) / 2;
            if (!enough(mi, m, n, k)) lo = mi + 1;
            else hi = mi;
        }
        return lo;
    }
}

The only doubt that I have is how come we are sure that this algorithm will give a answer which lies in multiplication table because not all numbers from [1:n*m] are present in the table. For example this is a table for n=5 and m=7 and has missing numbers like 11,22,23 etc. 1 2 3 4 5 6 7 2 4 6 8 10 12 14 3 6 9 12 15 18 21 4 8 12 16 20 24 28 5 10 15 20 25 30 35

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English 51stDimension 2021-08-07 14:39:00 1239 Initial revision (published)