KatInterview

668. Kth Smallest Number in Multiplication Table

HardSolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

The problem asks for the k-th smallest element in a conceptual m x n multiplication table. The table is defined by mat[i][j] = i * j. Given that m and n can be as large as 30,000, generating the full m * n table (which could have up to 900 million elements) and sorting it is not feasible due to memory and time constraints.

Solution

Kth Smallest Number in Multiplication Table in Python

class Solution:
    def findKthNumber(self, m: int, n: int, k: int) -> int:
        # The range of possible values is [1, m * n].
        # We can binary search on this range to find the k-th smallest number.
        low, high = 1, m * n

        while low < high:
            mid = low + (high - low) // 2
            
            # Calculate how many numbers in the multiplication table are less than or equal to mid.
            count = 0
            # We can optimize the count by making sure m is the smaller dimension.
            # If m > n, we can swap them, the multiplication table is symmetric.
            # But just iterating up to m is fine too.
            for i in range(1, m + 1):
                # For each row `i`, the numbers are i, 2*i, 3*i, ..., n*i.
                # The number of elements <= mid in this row is count of j such that i*j <= mid => j <= mid/i.
                # Since j cannot be more than n, the count for this row is min(n, mid // i).
                count += min(n, mid // i)

            if count < k:
                # If count is < k, mid is too small. The answer must be in the upper half.
                low = mid + 1
            else:
                # If count is >= k, mid might be the answer, but there could be a smaller one.
                # So, we search in the lower half, including mid.
                high = mid
        
        return low

Kth Smallest Number in Multiplication Table in Java

class Solution {
    public int findKthNumber(int m, int n, int k) {
        // The range of possible values is [1, m * n].
        // We can binary search on this range to find the k-th smallest number.
        int low = 1;
        int high = m * n;

        while (low < high) {
            int mid = low + (high - low) / 2;
            
            // Calculate how many numbers in the multiplication table are less than or equal to mid.
            int count = 0;
            // To optimize, we can iterate up to min(m, n) and use the other as max columns.
            // Here, we just iterate through rows of m.
            for (int i = 1; i <= m; i++) {
                // For each row `i`, the numbers are i, 2*i, 3*i, ..., n*i.
                // The number of elements <= mid in this row is count of j such that i*j <= mid => j <= mid/i.
                // Since j cannot be more than n, the count for this row is min(n, mid/i).
                count += Math.min(n, mid / i);
            }

            if (count < k) {
                // If count is < k, mid is too small. The answer must be in the upper half.
                low = mid + 1;
            } else {
                // If count is >= k, mid might be the answer, but there could be a smaller one.
                // So, we search in the lower half, including mid.
                high = mid;
            }
        }
        
        return low;
    }
}

Kth Smallest Number in Multiplication Table in C++

class Solution {
public:
    int findKthNumber(int m, int n, int k) {
        // The range of possible values is [1, m*n].
        // We can binary search on this range to find the k-th smallest number.
        int low = 1;
        int high = m * n;
        int ans = -1;

        while (low <= high) {
            int mid = low + (high - low) / 2;
            
            // Calculate how many numbers in the multiplication table are less than or equal to mid.
            int count = 0;
            for (int i = 1; i <= m; ++i) {
                // For each row `i`, the numbers are i, 2*i, 3*i, ..., n*i.
                // The number of elements <= mid in this row is count of j such that i*j <= mid => j <= mid/i.
                // Since j cannot be more than n, the count for this row is min(n, mid/i).
                count += std::min(n, mid / i);
            }

            if (count >= k) {
                // If count is >= k, mid might be the answer, but there could be a smaller one.
                // So, we store mid as a potential answer and search in the lower half.
                ans = mid;
                high = mid - 1;
            } else {
                // If count is < k, mid is too small. The answer must be in the upper half.
                low = mid + 1;
            }
        }
        
        return ans;
    }
};

Kth Smallest Number in Multiplication Table in TypeScript

function findKthNumber(m: number, n: number, k: number): number {
    // The range of possible values is [1, m * n].
    // We can binary search on this range to find the k-th smallest number.
    let low = 1;
    let high = m * n;

    while (low < high) {
        const mid = low + Math.floor((high - low) / 2);
        
        // Calculate how many numbers in the multiplication table are less than or equal to mid.
        let count = 0;
        for (let i = 1; i <= m; i++) {
            // For each row `i`, the numbers are i, 2*i, 3*i, ..., n*i.
            // The number of elements <= mid in this row is count of j such that i*j <= mid => j <= mid/i.
            // Since j cannot be more than n, the count for this row is min(n, mid/i).
            count += Math.min(n, Math.floor(mid / i));
        }

        if (count < k) {
            // If count is < k, mid is too small. The answer must be in the upper half.
            low = mid + 1;
        } else {
            // If count is >= k, mid might be the answer, but there could be a smaller one.
            // So, we search in the lower half, including mid.
            high = mid;
        }
    }
    
    return low;
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.