Skip to content

Adjacent Elements Product

EasyPython 3Arrays

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

The array has at least 2 elements. Elements may be negative, so the largest product can come from two negatives (e.g. -6 * -1 = 6) rather than the two largest positives.

Examples:
  [3, 6, -2, -5, 7, 3]  -> 21   (7 * 3)
  [5, 1, 2, 3, 1, 40]   -> 40    (1 * 40)
  [-1, -2]              -> 2     (-1 * -2)
  [9, 5, 10, 2, 24, -1, -48]  -> 50  (5 * 10)
  [1, 0, 1, 0, 1000]   -> 0     (best adjacent pair still multiplies to 0)

Python 3, run in your browser. Run tests runs the problem's own tests with a free account; the AI interviewer reads your code and your runs in a practice interview.

Loading the editor…