[Codewars] Block sequence

Block sequence

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int solve(long long n){
for(long long i = 1, sum = 1, add = 1, po = 10; n > sum;) {
n -= sum;
i += 1;
if(i == po) {
add += 1;
po *= 10;
}
sum += add;
}
for(long long i = 1, add = 1, po = 10;;) {
if(n > add) n -= add;
else {
return std::to_string(i)[n-1] - '0';
}
i += 1;
if(i == po) {
add += 1, po *= 10;
}
}
return -1;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/06/01/PS/Codewars/block-sequence/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.