BackmediumArrays Amazon

Galaxy Temperature Peaks Solution

Problem Statement

Given a sorted array of temperatures, find the index of the peak temperature. The temperatures are either increasing and then decreasing, or vice versa, around a single peak temperature.

Example 1
Input
[7, 7, 8, 7, 7]
Output
2

Explanation: Step-by-step: with input [7, 7, 8, 7, 7], we first check if the array is increasing or decreasing. Since it's increasing, we then check if the last element is the peak. If it is, we return the last index. Otherwise, we return the index of the peak element in the increasing part of the array.

Example 2
Input
[7, 8, 7, 7, 7]
Output
1

Explanation: Step-by-step: with input [7, 8, 7, 7, 7], we first check if the array is increasing or decreasing. Since it's decreasing, we then check if the first element is the peak. If it is, we return the first index. Otherwise, we return the index of the peak element in the decreasing part of the array.

Constraints

  • 1 <= temperatures.length <= 1000
  • -2^31 <= temperatures[i] <= 2^31 - 1
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