Code Lab

Modo entrevista. Cronômetro de 10 min, sem dicas e sem autocompletar

Largest product of two neighbors

MédiaArrays10 min

Write a function maxAdjacentProduct(numbers) that returns the largest product of two neighbor elements of an array.

  • numbers is an array of integers. They can be positive, negative or 0.
  • Two elements are neighbors when their indexes are i and i + 1. Multiply every such pair and return the largest product as a number.
  • Two negative numbers give a positive product: -5 * -6 is 30.
  • 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.

Exemplos

maxAdjacentProduct([3, 6, -2, -5, 7, 3])

Retorna 21

maxAdjacentProduct([-5, -6, 1])

Retorna 30

maxAdjacentProduct([5])

Retorna null

Cobrada em Amazon · Fonte

Carregando o editor...
  1. EntradamaxAdjacentProduct([ 3, 6, -2, -5, 7, 3 ])
    Esperado21
  2. EntradamaxAdjacentProduct([ 1, 2, 3 ])
    Esperado6
  3. EntradamaxAdjacentProduct([ 5 ])
    Esperadonull

Os testes ocultos rodam no envio