- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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
We are given an array of integers. We are required to find the pair of adjacent elements that have the largest product and return that product.
For example −
If the input array is −
const arr = [3, 6, -2, -5, 7, 3];
Then the output should be 21 because [7, 3] is the pair with the greatest sum.
Example
Following is the code −
const arr = [3, 6, -2, -5, 7, 3]; const adjacentElementsProduct = (arr = []) => { let prod, ind; for (ind = 1; ind < arr.length; ind++) { if (ind === 1 || arr[ind - 1] * arr[ind] > prod) { prod = arr[ind - 1] * arr[ind]; }; }; return prod; }; console.log(adjacentElementsProduct(arr));
Output
Following is the output on console −
21
- Related Articles
- Maximum product of any two adjacent elements in JavaScript
- Maximum decreasing adjacent elements in JavaScript
- Maximum product of 4 adjacent elements in matrix in C++
- Given an array of integers, find the pair of adjacent elements that has the largest product and return that product JavaScript
- Finding element greater than its adjacent elements in JavaScript
- Adjacent elements of array whose sum is closest to 0 - JavaScript
- Efficient algorithm for grouping elements and counting duplicates in JavaScript
- Multiply Adjacent elements in Python
- Python – Adjacent elements in List
- Pair of (adjacent) elements of an array whose sum is lowest JavaScript
- Compute cartesian product of elements in an array in JavaScript
- Finding the product of array elements with reduce() in JavaScript
- Comparing adjacent element and swap - JavaScript?
- Maximum sum of difference of adjacent elements in C++
- Digital root sort algorithm JavaScript

Advertisements