Find Local Maxima in Sensor Data
Problem statement
You are preparing sensor temperature readings for an anomaly-detection model. Given an integer array rawData and a positive integer localArea, find every local maximum.
An index i is a local maximum when the values strictly decrease as you move away from rawData[i] on both sides, for up to localArea adjacent values on each side. Stop at the array boundary when fewer values are available.
- To the left, require
rawData[j] > rawData[j - 1]for everyjfromidown tomax(0, i - localArea) + 1. - To the right, require
rawData[j] > rawData[j + 1]for everyjfromithroughmin(rawData.length - 1, i + localArea) - 1. - Both sides must satisfy their conditions. A side with no available neighbors automatically satisfies its condition.
Return the 0-based indices of all local maxima in ascending order. If none qualify, return an empty array.
Function
findLocalMaxima(rawData: int[], localArea: int) → int[]Examples
Example 1
rawData = [2, 10, 4, 3, 11, 5, 2, 6, 12, 3, 2]localArea = 2return = [1, 8]At index 1, the right chain is 10 > 4 > 3, and the only available left neighbor gives 10 > 2. At index 8, the chains are 12 > 6 > 2 to the left and 12 > 3 > 2 to the right.
Index 4 fails despite 11 > 5 > 2 on the right: its left chain would require 11 > 3 > 4, which is false. The result is [1, 8].
Example 2
rawData = [4, 3, 2, 1]localArea = 2return = [0]Index 0 has no left neighbors, and its required right chain is 4 > 3 > 2. Every later index has a larger immediate left neighbor, so the result is [0].
Example 3
rawData = [1, 3, 3, 1]localArea = 1return = []The two middle readings are equal, so neither satisfies strict decrease toward the other. The endpoints each have a larger neighbor. No index qualifies.
Constraints
- For this exercise, assume
1 <= rawData.length <= 10^5. - For this exercise, assume
-10^9 <= rawData[i] <= 10^9. - For this exercise, assume
1 <= localArea <= 10^5; it may exceed the array length.