-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongDigit.java
More file actions
33 lines (31 loc) · 755 Bytes
/
LongDigit.java
File metadata and controls
33 lines (31 loc) · 755 Bytes
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
public class LongDigit {
static long check(long X) {
long sum = 0;
while (X > 0) {
sum += X;
X /= 10;
}
return sum;
}
// Binary search logic
public static long findX(long S) {
long high = S;
long low = 0;
while (high - low > 1) {
long mid = (high + low) / 2;
if (check(mid) > S) {
high = mid;
} else {
low = mid;
}
}
for (long i = Math.max(low - 10, 0); i < high + 10; i++) {
if (check(i) == S)
return i;
}
return -1;
}
public static void main(String[] args) {
System.out.println(findX(3000));
}
}