code.ashish.me

Atom feed

Recently added: 02 Count Of Subset Sum, 416 Partition Equal Subset Sum, 01 Subset Sum, 518 Coin Change 2, 983 Minimum Cost For Tickets

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:35:16+05:18 · Edit