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


Our work in this problem statement is to find the pair of adjacent elements that has the largest product and return the product if we have given an array. And we have to implement this problem with the help of Javascript functionalities.

Logic for the given problem

In the given problem statement we have to calculate the product of the largest elements in the array and show the resultant output to the console. So we will implement this problem with two methods: first is by using the infinity and second without using infinity. Infinity is a global object. The value of infinity is greater than any number. And if we use minus infinity it means it is the smallest number. In the second method we will first initialize the product of the first two elements and check its values with all products if it comes greater than this product so return its value.

Algorithm - With the help of Infinity

Step 1 − At the start of the code declare an array of integer numbers.

Step 2 − involves defining a function called findProduct that takes an input array as its argument and returns the product of the array's largest elements.

Step 3 − We initialize a variable to store both the largest element and the resulting product.

Step 4 − We begin a for loop to iterate through each element of the array, using the multiplication operator to calculate the product of adjacent elements.

Step 5 − We will update its value with the new product value following the above step if the outcome product of the above step is greater than the largest product defined in step 3.

Step 6 − Finally, show the output of the largest product of the largest items in the array.

Code for the algorithm - With the help of Infinity

const arr = [1, 2, 3, 4, 5];
//function to calculate the largest product of adjacent items
const findProduct = (arr = []) => {
   //variable to store largest product
   var 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(findProduct(arr));

Algorithm - Without infinity

Step 1 − This step Involves creating an array from which we will compute the largest product of two adjacent elements.

Step 2 − We define a function to determine the largest product of two adjacent elements in the array.

Step 3 − We declare the largest product value by calculating the product of the first two adjacent elements.

Step 4 − Begins with initializing the largest product's value and starting a for loop. The for loop traverses the array and uses the multiplication operator to calculate the product of adjacent elements. If the product of two adjacent elements is greater than the current largest product value, we add the new largest value to the variable defined in Step3.

Step 5 − Finally, this step shows the largest product of the two largest adjacent elements.

Code for the algorithm - Without infinity

const arr = [5, 6, 7, 8, 9];
//function to calculate the largest product
const findLargestProduct = (arr = []) => {
   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(findLargestProduct(arr));

Complexity

The time and space complexities for the first approach, which is used to determine the product of two adjacent elements, are O(n) and O(1), respectively.

The reason for the O(n) time complexity of the code is the use of a single for loop to calculate the product of adjacent elements in the input array. Moreover, the code's memory usage is constant, which results in a space complexity of O(1).

Regarding the second code: O(n^2) and O(n) are the respective time and space complexities.

In the second code, two for loops are utilized to work out the partial sums of the adjacent items in the given array. So both the loops are traversing the full array so the time complexity of this code is O(n^2), n is the size of the input array. And the code also uses an array to store the partial sums, which has a space complexity of O(n).

Conclusion

The above codes efficiently finds the largest product of any adjacent items in the array. As we can see the time complexity for the first method is O(n) so it is efficient as compared to the second method with time complexity O(n^2), which is less efficient. So overall its important factor to use time and space complexities of the code to optimize the performance of the algorithm.

Updated on: 18-May-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements