4331. K-th Digit in Infinite String

Medium
Math
Binary Search

Description

You are given an integer k.

An infinite string is formed by concatenating the decimal representations of the positive integers, without separators.

For every nonnegative integer b, block b contains the positive integers from 10 * b through 10 * b + 9. The integers in each block are appended as follows:

  • If b is even, append the integers in increasing order.
  • If b is odd, append the integers in decreasing order.

Therefore, the string starts with the integers 1 through 9, followed by 19 through 10, then 20 through 29, then 39 through 30, and so on.

Return the kth digit (1-indexed) of this string.

 

Example 1:

Input: k = 4

Output: 4

Explanation:

The string begins as "123456789..". The 4th digit is '4'.

Example 2:

Input: k = 15

Output: 7

Explanation:

The string begins as "123456789191817..". The 15th digit is '7'.

Example 3:

Input: k = 11

Output: 9

Explanation:

The string begins as "12345678919..". The 11th digit is '9'.

 

Constraints:

  • 1 <= k <= 1015

Hints

Hint 1
<p>The order inside a block does not affect its total number of digits. After finishing block <code>b</code>, the string has used exactly the same number of digits as writing all integers from 1 through <code>10 * b + 9</code> in the usual order.</p>
Hint 2
<p>Write a function that computes the total number of decimal digits needed to write all integers from 1 through <code>x</code>. Compute it by grouping numbers according to their digit length.</p>
Hint 3
<p>Binary search for the first block <code>b</code> whose cumulative number of digits is at least <code>k</code>. Then subtract the number of digits before that block to obtain the 1-indexed position inside the block.</p>
Hint 4
<p>For <code>b &gt; 0</code>, all 10 integers in the block have the same number of digits. Use the position inside the block to determine which integer and which digit it refers to. If <code>b</code> is odd, remember that the integers appear in decreasing order.</p>

Similar Questions

Statistics

Acceptance
35.5%
Submissions
32,139
Accepted
11,421