Todos los desafíos
Intermedio 0 resueltos 0% de aceptación

Champernowne Digits

Find digits at given positions of the string 123456789101112... made by writing out the counting numbers.

Lo que aprenderás

  • Count digits used by numbers of each length
  • Locate a number and digit from a position
  • Use 64-bit arithmetic safely

Problema

Ver traducción

Write the positive integers one after another: 123456789101112131415... Position 1 holds 1, position 10 holds the 1 of 10, position 11 the 0 of 10. For each requested position, find the digit there. The positions go far past what you could build as a string: skip whole blocks of numbers with the same number of digits (9 one-digit numbers, 90 two-digit numbers, 900 three-digit numbers, ...).

Implement champernowne_digits(positions).

Input

positions: Positions in the digit string, counting from 1.

Output

Return the digit at each position, in the same order as the input. An empty input returns an empty list.

Restricciones

0 ≤ positions.length ≤ 100; 1 ≤ position ≤ 10^15

Ejemplos

Ejemplo 1
Entrada:
positions = [1, 9, 10, 11]

Salida esperada:
expected = [1, 9, 1, 0]
Entrada:
std::vector<long long> positions = {1, 9, 10, 11};

Salida esperada:
std::vector<long long> expected = {1, 9, 1, 0};
Entrada:
long[] positions = new long[] {1, 9, 10, 11};

Salida esperada:
long[] expected = new long[] {1, 9, 1, 0};

Explicación

The string starts 1 2 3 4 5 6 7 8 9 1 0 1 1, so the digits are [1, 9, 1, 0].

Ejemplo 2
Entrada:
positions = [15, 190]

Salida esperada:
expected = [2, 1]
Entrada:
std::vector<long long> positions = {15, 190};

Salida esperada:
std::vector<long long> expected = {2, 1};
Entrada:
long[] positions = new long[] {15, 190};

Salida esperada:
long[] expected = new long[] {2, 1};

Explicación

Position 15 is the 2 of 12. Positions 10 to 189 hold 10 to 99, so 190 is the first digit of 100: [2, 1].

Cómo abordarlo

Inténtalo primero por tu cuenta y luego abre las pistas una a una.

Pista 1

Numbers with d digits take 9 × 10^(d − 1) × d positions in total.

Pista 2

Subtract whole blocks until the position falls inside one.

Pista 3

Within the block, (position − 1) / d tells you which number and (position − 1) % d which digit.

Complejidad esperada

O(m × log P) time for m positions, O(m) extra space

Referencias

Este reto está adaptado para CodeUtility. Sigue el enunciado y las restricciones indicadas arriba.

Clasificación

Clasificación completa

La mejor solución aprobada de cada programador. Las más rápidas, ligeras y mejor valoradas van primero.

Aún no hay soluciones aprobadas. Resuélvelo y quédate el primer puesto.

Aprende de otros

Soluciones de la comunidad

Aún no hay soluciones publicadas. Resuelve este reto para compartir la primera.