JavaScript: Adjacent Elements Product Algorithm


In the given problem statement we have to calculate the adjacent elements product with the help of Javascript functionalities. So we will use a loop and basic mathematical operations to solve this problem.

Understanding the problem

The problem statement at hand is to calculate the product of two adjacent elements and the product should be maximum in the array. So basically the algorithm involves the maximum product of two adjacent items in the array. This functionality is commonly used in various cases like finding the maximum profit or solving the mathematical puzzles.

The example of the above problem is: suppose we have an array like [1, 2, 3, 4, 5, 6] so the maximum product in this array will be 5 * 6 = 30. So our task is to implement the code to generate the maximum product of two adjacent items in the array.

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 will 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

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. And 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 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 maxProduct value with the product value. And also return the value of maxProduct to show it on the console.

Example

//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

Complexity

The time complexity of the function we have created is 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. The space complexity is 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.

Updated on: 14-Aug-2023

475 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements