Intuition
Build the entire multiplication table as a flat list, sort it, and return the k-th element.
Explanation
Brute Force
Generate all m * n values by computing i * j for each cell (i from 1 to m, j from 1 to n). Sort the resulting list and return the element at index k - 1.
Sorting m * n values costs O(mn log(mn)) time and O(mn) space.
Why it is slow: For m = n = 30000, the table has 9 * 10^8 entries — far too many to store and sort.
Implementation
import java.util.ArrayList;
import java.util.Collections;
class Solution {
public int findKthNumber(int m, int n, int k) {
ArrayList<Integer> table = new ArrayList<>();
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
table.add(i * j);
}
}
Collections.sort(table);
return table.get(k - 1);
}
}