
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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).
Logic for the given problem
To solve the above problem we will iterate the input array and calculate the product of every pair of adjacent items. We will keep track of the maximum product found and update when we find the larger product. The function will be started from the first item and we will stop the calculation at the second to last element because there are no items present after it has formed a pair.
Algorithm
Following are the steps to calculate the adjacent element's products ?
-
Step 1: As per the given problem we have to find the maximum product of two adjacent items present in the array. So first we will define a function to do the same task. This function will accept a parameter as input called inputArray.
-
Step 2: After defining the above function we will declare a variable to store the maximum product of two items. And initialize its value to a number.Negative_Infinity.
-
Step 3: We have defined the variable to store the maximum product. Now our task is to use a for loop to iterate the array items and calculate the product of two adjacent items.
-
Step 4: As we have to find the product, in this step we will calculate the product of two adjacent items. And store product value in a separate variable called product.
- Step 5: At this step, we will check the condition that the value of the product is the maximum product or not. If the condition is true then we will update the maxProduct value with the product value. And also return the value of maxProduct to show it on the console.
maxProduct set to Number.NEGATIVE_INFINITY?
let maxProduct = Number.NEGATIVE_INFINITY;
Update maxProduct if a larger product is found using the if condition ?
if (product > maxProduct) {maxProduct = product; }
Example
Below is an example of calculating the adjacent element's products in JavaScript ?
//Function for adjacent elements product function adjacentElemsProduct(inputArray) { let maxProduct = Number.NEGATIVE_INFINITY; for (let i = 0; i < inputArray.length - 1; i++) { const product = inputArray[i] * inputArray[i + 1]; if (product > maxProduct) { maxProduct = product; } } return maxProduct; } const array = [3, 6, -2, -5, 7, 3]; const result = adjacentElemsProduct(array); console.log(result);
Output
21
For the array [3, 6, -2, -5, 7, 3], let's calculate the product of adjacent elements and track the largest one ?
- 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)
So, the function returns 21 as the largest product.
Complexity
Time Complexity: O(n), in which n is the length of the input array. Because we have performed a single pass through the array. And we have performed comparison and updation of the maximum product in a constant time.
Space complexity: O(1) because we have used a fixed amount of memory to store the maximum product.
Conclusion
The problem for calculating the maximum product of two adjacent items is a simple and powerful algorithm. We have provided an algorithm and calculated its time and space complexity. This function can be useful in various cases for finding the maximum product, financial analysis, and puzzle solving.