mediumArraysPattern: Sliding Window

Peak Energy Indices Solution

Problem Statement

Given an array of integers `energyReadings`, find all indices `i` where the value at index `i` is greater than or equal to its neighboring values. If the value is at the edge of the array, it only needs to be greater than or equal to its one neighboring value.

Examples

Example 1:
Input:[1, 3, 2, 5, 4]
Output:[1, 3]
Explanation: The energy readings at indices 1 and 3 are greater than or equal to their neighboring readings
Example 2:
Input:[5, 4, 3, 2, 1]
Output:[0]
Explanation: The energy reading at index 0 is greater than or equal to its one neighbor

Constraints

  • 1 <= length of array <= 10^5
  • -10^5 <= each energy reading <= 10^5
Time: O(n) Space: O(n)
The optimal approach involves iterating over the array once and comparing each element with its neighbors, resulting in a solution with O(n) time complexity. This approach takes advantage of the fact that each element only needs to be compared with its immediate neighbors.

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.