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

Finding the pair of adjacent elements with the largest product is a common array problem in JavaScript. This involves iterating through the array and comparing products of consecutive elements.

Problem Understanding

Given an array of integers, we need to find two adjacent elements whose product is maximum among all possible adjacent pairs. For example, in array [1, 2, 3, 4], the adjacent pairs are (1,2), (2,3), and (3,4) with products 2, 6, and 12 respectively.

Method 1: Using -Infinity for Initialization

This approach initializes the maximum product with -Infinity to handle arrays with negative numbers correctly.

const arr = [1, 2, 3, 4, 5];

// Function to find largest product of adjacent elements
const findProduct = (arr = []) => {
   // Initialize with negative infinity to handle negative numbers
   let largest = -Infinity;
   
   for(let i = 0; i < arr.length - 1; i++){
      const product = arr[i] * arr[i + 1];
      if(product > largest){
         largest = product;
      }
   }
   return largest;
};

console.log("Array:", arr);
console.log("Largest adjacent product:", findProduct(arr));
Array: [ 1, 2, 3, 4, 5 ]
Largest adjacent product: 20

Method 2: Using First Pair as Initial Maximum

This method initializes the maximum product with the first adjacent pair's product, then compares with remaining pairs.

const arr = [5, 6, 7, 8, 9];

// Function to find largest product without using infinity
const findLargestProduct = (arr = []) => {
   // Initialize with first pair's product
   let largestProduct = arr[0] * arr[1];
   
   for(let i = 1; i < arr.length - 1; i++){
      const product = arr[i] * arr[i + 1];
      if(product > largestProduct){
         largestProduct = product;
      }
   }
   return largestProduct;
};

console.log("Array:", arr);
console.log("Largest adjacent product:", findLargestProduct(arr));
Array: [ 5, 6, 7, 8, 9 ]
Largest adjacent product: 72

Handling Edge Cases

Let's test with arrays containing negative numbers to see why -Infinity initialization is preferred:

const negativeArray = [-5, -2, -8, -1];

console.log("Negative array:", negativeArray);
console.log("Using -Infinity method:", findProduct(negativeArray));

// Test what happens with all negative products
const allNegative = [-1, -2, -3, -4];
console.log("All negative array:", allNegative);
console.log("Largest product:", findProduct(allNegative));
Negative array: [ -5, -2, -8, -1 ]
Using -Infinity method: 10
All negative array: [ -1, -2, -3, -4 ]
Largest product: 12

Comparison of Methods

Method Time Complexity Space Complexity Handles Negatives
Using -Infinity O(n) O(1) Yes
Using First Pair O(n) O(1) Limited

Complete Solution with Validation

function findLargestAdjacentProduct(arr) {
   // Validate input
   if (!arr || arr.length < 2) {
      throw new Error("Array must have at least 2 elements");
   }
   
   let maxProduct = -Infinity;
   let maxIndex = 0;
   
   for(let i = 0; i < arr.length - 1; i++) {
      const product = arr[i] * arr[i + 1];
      if(product > maxProduct) {
         maxProduct = product;
         maxIndex = i;
      }
   }
   
   return {
      product: maxProduct,
      pair: [arr[maxIndex], arr[maxIndex + 1]],
      indices: [maxIndex, maxIndex + 1]
   };
}

// Test the enhanced solution
const testArray = [3, 6, -2, -5, 7, 3];
const result = findLargestAdjacentProduct(testArray);

console.log("Array:", testArray);
console.log("Result:", result);
Array: [ 3, 6, -2, -5, 7, 3 ]
Result: { product: 21, pair: [ 7, 3 ], indices: [ 4, 5 ] }

Conclusion

Both methods have O(n) time complexity and O(1) space complexity. The -Infinity approach is more robust as it correctly handles arrays with all negative products, making it the preferred solution for this problem.

Updated on: 2026-03-15T23:19:00+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements