Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
JavaScript: Adjacent Elements Product Algorithm
In this article, we will learn to calculate the adjacent elements product with the help of JavaScript functionalities. This problem is often asked in coding interviews or algorithm challenges, and it tests one's understanding of array manipulation and performance optimization.
Problem Statement
Given an array of integers, your task is to return the largest product that can be obtained by multiplying any two adjacent numbers in the array.
For example ?
Input:
[5, 1, 2, 3, 1]
Output:
6
The adjacent elements are (5, 1), (1, 2), (2, 3), and (3, 1). The largest product is 6 (from 2 * 3).
Algorithm Overview
To solve this problem, we iterate through the input array and calculate the product of every pair of adjacent elements. We keep track of the maximum product found and update it when we find a larger product. The iteration starts from the first element and stops at the second-to-last element since the last element has no adjacent pair.
Step-by-Step Algorithm
Following are the steps to calculate the adjacent elements product:
- Step 1: Define a function that accepts an input array parameter.
-
Step 2: Initialize a variable to store the maximum product with
Number.NEGATIVE_INFINITY. - Step 3: Use a for loop to iterate through the array elements.
- Step 4: Calculate the product of two adjacent elements for each iteration.
- Step 5: Compare the current product with the maximum product and update if necessary.
- Step 6: Return the maximum product found.
Implementation
Here's the complete implementation of the adjacent elements product algorithm:
function adjacentElementsProduct(inputArray) {
// Initialize with negative infinity to handle negative numbers
let maxProduct = Number.NEGATIVE_INFINITY;
// Iterate through array, stopping before the last element
for (let i = 0; i < inputArray.length - 1; i++) {
// Calculate product of current and next element
const product = inputArray[i] * inputArray[i + 1];
// Update maximum if current product is larger
if (product > maxProduct) {
maxProduct = product;
}
}
return maxProduct;
}
// Test with multiple examples
const array1 = [3, 6, -2, -5, 7, 3];
const array2 = [5, 1, 2, 3, 1];
const array3 = [-1, -2, -3, -4];
console.log("Array [3, 6, -2, -5, 7, 3]:", adjacentElementsProduct(array1));
console.log("Array [5, 1, 2, 3, 1]:", adjacentElementsProduct(array2));
console.log("Array [-1, -2, -3, -4]:", adjacentElementsProduct(array3));
Array [3, 6, -2, -5, 7, 3]: 21 Array [5, 1, 2, 3, 1]: 6 Array [-1, -2, -3, -4]: 12
Example Walkthrough
For the array [3, 6, -2, -5, 7, 3], let's trace through the algorithm:
- 3 × 6 = 18 ? maxProduct = 18
- 6 × (-2) = -12 ? maxProduct = 18 (no change)
- (-2) × (-5) = 10 ? maxProduct = 18 (no change)
- (-5) × 7 = -35 ? maxProduct = 18 (no change)
- 7 × 3 = 21 ? maxProduct = 21 (updated)
The function returns 21 as the largest product.
Edge Cases
The algorithm handles several important edge cases:
-
Negative numbers: Using
Number.NEGATIVE_INFINITYensures even negative products are properly compared - Mixed positive/negative: The algorithm correctly identifies the maximum product regardless of sign combinations
- All negative numbers: Returns the least negative (closest to zero) product
Complexity Analysis
Time Complexity: O(n), where n is the length of the input array. We perform a single pass through the array with constant-time operations for each element.
Space Complexity: O(1) because we use only a fixed amount of extra memory regardless of input size.
Conclusion
The adjacent elements product algorithm is an efficient solution for finding the maximum product of consecutive array elements. With O(n) time complexity and O(1) space complexity, it's optimal for this problem and demonstrates fundamental array traversal techniques commonly used in coding interviews.
