- 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

# Finding element greater than its adjacent elements in JavaScript

We are required to write a JavaScript function that takes in an array of numbers as the first and the only argument.

The function should find and return one such number from the array which is greater than both, the number on its immediate right and the number on its immediate left. If there exists more than one such element in the array, our function should return any one of them.

For example −

If the input array is −

const arr = [3, 6, 7, 9, 8, 2, 5];

Then the output should be −

const output = 9;

Since the question demands finding the peak element, we can use a tweaked version of the binary search algorithm.

The steps for the same will be −

Look at any element.

If the next element and the previous elements are both less than the current, we find a solution, then return the index of current.

If the next element is greater than the current, there must be a peak to the right, look recursively to the right.

If the previous element is greater than current, there must be a peak to the left, look recursively to the left.

## Example

Following is the code −

const arr = [3, 6, 7, 9, 8, 2, 5]; const greaterThanAdjacent = (arr = [], start = 0, end = arr.length) => { let mid = start + Math.floor((end - start) / 2); let curr = arr[mid]; let prev = mid-1 < 0 ? -Infinity : arr[mid-1]; let next = mid+1 > arr.length-1 ? -Infinity : arr[mid+1]; if (curr > prev && curr > next){ return arr[mid]; } if (curr < next){ return greaterThanAdjacent(arr, mid+1, end); } if (curr > next){ return greaterThanAdjacent(arr, start, mid-1); } return null; }; console.log(greaterThanAdjacent(arr));

## Output

Following is the console output −

9

- Related Questions & Answers
- Python – Sort Matrix by Number of elements greater than its previous element
- Finding the element larger than all elements on right - JavaScript
- Finding distance to next greater element in JavaScript
- Elements greater than the previous and next element in an Array in C++
- Retaining array elements greater than cumulative sum using reduce() in JavaScript
- Find smallest element greater than K in Python
- JavaScript: Adjacent Elements Product Algorithm
- Python program to print Rows where all its Elements’ frequency is greater than K
- Maximum decreasing adjacent elements in JavaScript
- Rearrange an array such that every odd indexed element is greater than its previous in C++
- Comparing adjacent element and swap - JavaScript?
- Find Smallest Letter Greater Than Target in JavaScript
- Count subarrays with all elements greater than K in C++
- Mask array elements greater than a given value in Numpy
- Finding next greater node for each node in JavaScript