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

Largest Adjacent-Digit Product

Take every contiguous window of span digits and multiply its digits.

Problema

Take every contiguous window of span digits and multiply its digits. Return the largest product. Leading zeros are ordinary digits; a window containing zero has product zero. Do not reorder or skip digits.

Implement largest_adjacent_digit_product(digits, span).

Input

digits: The digit sequence, including any leading zeros.

span: The exact number of adjacent digits in a window.

Output

Return the greatest product over all windows of this width.

Restricciones

1 ≤ digit-string length ≤ 100,000. Characters are 0…9. 1 ≤ span ≤ min(18, length). All products fit signed 64-bit.

Ejemplos

Ejemplo 1
Entrada:
span = 2
digits = "23405"

Salida esperada:
expected = 12
Entrada:
long long span = 2;
std::string digits = "23405";

Salida esperada:
long long expected = 12;
Entrada:
long span = 2;
string digits = "23405";

Salida esperada:
long expected = 12;

Explicación

The windows 23, 34, 40, and 05 have products 6, 12, 0, and 0. The largest is 12.

Ejemplo 2
Entrada:
span = 3
digits = "102"

Salida esperada:
expected = 0
Entrada:
long long span = 3;
std::string digits = "102";

Salida esperada:
long long expected = 0;
Entrada:
long span = 3;
string digits = "102";

Salida esperada:
long expected = 0;

Explicación

There is only one width-three window. It contains zero, so 1 × 0 × 2 = 0.

Referencias

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

Aprende de otros

Soluciones de la comunidad

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