
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Given an array of integers, find the pair of adjacent elements that has the largest product and return that product JavaScript
We are required to write a JavaScript function that takes in an array of Numbers (positive and negative numbers).
The function should return the highest product of two adjacent elements of the array.
For example −
If the input array is −
const arr = [-23, 4, -3, 8, -12];
Then the output should be −
const output = -12;
and the two elements are 4 and -3
Example
const arr = [-23, 4, -3, 8, -12]; const adjacentProduct = (arr = []) => { let first = 0; let second = 0; let res = Number.MIN_SAFE_INTEGER; for (let i = 0; i < arr.length; i++) { first = arr[i]; second = arr[i + 1]; if (first * second > res) { res = first * second; }; }; return res; }; console.log(adjacentProduct(arr));
Output
This will produce the following output −
-12
- Related Questions & Answers
- JavaScript: Adjacent Elements Product Algorithm
- Find a pair with maximum product in array of Integers in C++
- Maximum product of any two adjacent elements in JavaScript
- Find the largest interval that contains exactly one of the given N integers In C++
- Find integers that divides maximum number of elements of the array in C++
- Finding the Largest Triple Product Array in JavaScript
- Pair of (adjacent) elements of an array whose sum is lowest JavaScript
- Program to find the largest product of two distinct elements in Python
- Rearrange an array to minimize sum of product of consecutive pair elements in C++
- Finding the product of array elements with reduce() in JavaScript
- Compute cartesian product of elements in an array in JavaScript
- Return the greatest possible product of n numbers from the array in JavaScript
- Return the cumulative product of array elements treating NaNs as one in Python
- Check if the array has an element which is equal to product of remaining elements in Python
- Maximum product of 4 adjacent elements in matrix in C++
Advertisements