code.ashish.me

Atom feed

Recently added: 128 Longest Consecutive Sequence, 347 Top K Frequent Elements, 045 Jump Game 2, 228 Summary Ranges, 219 Contains Duplicate 2

005 Adjacend Elements Product

/**
 *
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

/* Given an array of integers, find the pair of adjacent elements that has the largest product and return that product.

Example

For inputArray = [3, 6, -2, -5, 7, 3], the output should be adjacentElementsProduct(inputArray) = 21.

7 and 3 produce the largest product.
 */

function adjacedElementsProduct(values) {
  let max = values[0] * values[1]
  for (let index = 1; index < values.length - 1; index++) {
    const productOfAdjacentElements = values[index] * values[index + 1]
    max = max < productOfAdjacentElements ? productOfAdjacentElements : max
  }
  return max
}

test('adjaced Elements Product', () => {
  expect(adjacedElementsProduct([3, 6, -2, -5, 7, 3])).toEqual(21)
})

Created 2019-12-05T02:47:16+05:30 · Edit