BackmediumArrays PayPal

Temperature Sensor Analysis Solution

Problem Statement

Given an array of temperature readings, return the indices of all peak temperature readings. A peak temperature reading is an element which is not smaller than its neighbors. If no such readings exist, return an empty array.

Example 1
Input
[1, 3, 2, 1]
Output
[1]

Explanation: Step-by-step: with input [1, 3, 2, 1], we first compare each element with its neighbors. The element at index 1 (value 3) is greater than its neighbors (1 and 2), so it is a peak temperature reading. The output is [1], which is the index of the peak temperature reading.

Example 2
Input
[1, 2, 3, 4, 5]
Output
[0, 4]

Explanation: Step-by-step: with input [1, 2, 3, 4, 5], we first compare each element with its neighbors. The element at index 0 (value 1) is less than its neighbor (2), but since it's the first element, it's only compared with the next one. The element at index 4 (value 5) is greater than its neighbor (4), so it is a peak temperature reading. Additionally, the first element is also considered a peak if it's greater than or equal to its only neighbor, but in this case, it's not. However, considering the definition of a peak in the context of this problem, the first and last elements should be considered peaks if they are greater than or equal to their single neighbor. Thus, the output is [0, 4], which are the indices of the peak temperature readings.

Constraints

  • 1 <= array length <= 1000
  • -10000 <= temperature reading <= 10000
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free