Modo entrevista. Temporizador de 10 min, sin pistas y sin autocompletado
Largest product of two neighbors
MediaArrays10 min
Write a function maxAdjacentProduct(numbers) that returns the largest product of two neighbor elements of an array.
numbersis an array of integers. They can be positive, negative or0.- Two elements are neighbors when their indexes are
iandi + 1. Multiply every such pair and return the largest product as a number. - Two negative numbers give a positive product:
-5 * -6is30. - All products can be negative. Then return the largest of them, for example
-2. - If the array has fewer than two elements, there are no pairs: return null.
Ejemplos
maxAdjacentProduct([3, 6, -2, -5, 7, 3])Devuelve 21
maxAdjacentProduct([-5, -6, 1])Devuelve 30
maxAdjacentProduct([5])Devuelve null
Preguntada en Amazon · Fuente
Cargando el editor…
- Entrada
maxAdjacentProduct([ 3, 6, -2, -5, 7, 3 ])Esperado21 - Entrada
maxAdjacentProduct([ 1, 2, 3 ])Esperado6 - Entrada
maxAdjacentProduct([ 5 ])Esperadonull
Las pruebas ocultas se ejecutan al enviar