-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC440.java
More file actions
51 lines (43 loc) · 1.09 KB
/
LC440.java
File metadata and controls
51 lines (43 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
* LC440
*/
import java.util.*;
public class LC440 {
private static int countSteps(int curr, int n) {
long steps = 0;
long first = curr;
long last = curr;
while (first <= n) {
steps += Math.min(last, n) - first + 1;
first *= 10;
last = last * 10 + 9;
}
return (int) steps;
}
public static int findKthNumber(int n, int k) {
int curr = 1;
k--;
while (k > 0) {
int steps = countSteps(curr, n);
if (steps <= k) {
k -= steps;
curr++;
} else {
curr *= 10;
k--;
}
}
return curr;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter n Size : ");
int n = sc.nextInt();
System.out.print("Enter Find Element k index : ");
int k = sc.nextInt();
System.out.println();
int ans = findKthNumber(n, k);
System.out.println(ans);
sc.close();
}
}