Prefix Tree
Role of countSteps
countStepspublic int findKthNumber(int n, int k) {
int curr = 1;
k--;
while (k > 0) {
int step = countSteps(n, curr, curr + 1);
if (step <= k) {
curr++;
k -= step;
} else {
curr *= 10;
k--;
}
}
return curr;
}
// To count how many numbers exist between prefix1 and prefix2
private int countSteps(int n, long prefix1, long prefix2) {
int steps = 0;
while (prefix1 <= n) {
steps += Math.min(prefix2, n + 1) - prefix1;
prefix1 *= 10;
prefix2 *= 10;
}
return steps;
}Last updated