easyArraysPattern: Linear Scan

Peak Elevation Divergence Solution

Problem Statement

Given an array of integers representing sequential values, identify the count of elements that are strictly greater than both their immediate left neighbor and their immediate right neighbor. For the purpose of this calculation, the first and last elements of the array do not qualify as peaks, as they lack a complete set of neighbors.

Examples

Example 1:
Input:[1, 3, 2, 4, 1]
Output:1
Explanation: Only the element 3 is strictly greater than 1 and 2. The element 4 is greater than 2 and 1, but is not considered a peak because it lacks a right neighbor that satisfies the condition in the array index bounds.
Example 2:
Input:[1, 2, 3, 4, 5]
Output:0
Explanation: There are no elements that are strictly greater than both their immediate neighbors.
Example 3:
Input:[5, 6, 4, 7, 3, 8, 2]
Output:2
Explanation: The elements 6 (greater than 5 and 4) and 7 (greater than 4 and 3) are the only peaks.

Constraints

  • 3 <= array.length <= 1000
  • -10^4 <= array[i] <= 10^4
Time: O(n) Space: O(1)
Master coding challenges related to Arrays and solve the Peak Elevation Divergence problem optimally.

Run, Test & Submit Code

Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.

Solve on Interactive Workspace

Tested Solutions

No solution code is currently loaded.
Complete this code in the workspace editor.