mediumArraysPattern: Pattern recognition and iteration

Local Maxima Indices Solution

Problem Statement

You are given an array of integers `radiation_intensities`. Identify all indices of local maxima where the intensity is greater than or equal to its neighboring values. For edge cases, consider the intensity as a local maxima if it is greater than or equal to its single neighboring value.

Examples

Example 1:
Input:[1, 3, 2, 4, 5, 4, 3, 6, 5]
Output:[1, 3, 6, 7]
Explanation: Radiation intensities at indices 1, 3, 6, and 7 are local maxima
Example 2:
Input:[5, 4, 3, 2, 1]
Output:[0]
Explanation: Radiation intensity at index 0 is a local maxima

Constraints

  • The input array will have a minimum of 1 element and a maximum of 1000 elements.
  • All elements in the array will be integers between 1 and 1000.
Time: O(n) Space: O(1)
A more efficient approach involves iterating through the array only once, comparing each element to its immediate neighbors to identify local maxima, resulting in a time complexity of O(n). This approach takes advantage of the fact that a local maxima must be greater than or equal to its neighboring values.

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.