BackmediumArraysPhonePeZomato

Peak Temperature Indices Solution

Problem Statement

Given a sequence of temperature values stored in the array temperatures, find the indices of all temperature readings that exceed their immediate neighbors. If no such readings exist, return an empty list.

Example 1
Input
[10, 20, 12, 15, 18, 12, 15, 18, 12, 15, 18, 12]
Output
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Explanation: Step-by-step: Given the input [10, 20, 12, 15, 18, 12, 15, 18, 12, 15, 18, 12], we first initialize an empty list to store the indices of temperature readings that exceed their immediate neighbors. Then, we iterate through the input array. For each element, we check if it exceeds its immediate neighbor. If it does, we append its index to the list. Finally, we return the list. The output [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] indicates that the temperature readings at indices 1, 2, 3, 4, 5, 6, 7, 8, 9, and 10 all exceed their immediate neighbors.

Example 2
Input
[10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
Output
[]

Explanation: Step-by-step: Given the input [10, 10, 10, 10, 10, 10, 10, 10, 10, 10], we first initialize an empty list to store the indices of temperature readings that exceed their immediate neighbors. Then, we iterate through the input array. For each element, we check if it exceeds its immediate neighbor. Since all elements are equal, none of them exceed their immediate neighbors. Therefore, we return an empty list.

Constraints

  • 1 <= array length <= 1000
  • -10000 <= temperature reading <= 10000
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

šŸš€ Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Peak Temperature Indices — Problem Statement & Solution Guide

ArraysMediumArray traversal and pattern recognition
TimeO(n)
|
SpaceO(1)

Problem Description

Given a sequence of temperature values stored in the array temperatures, find the indices of all temperature readings that exceed their immediate neighbors. If no such readings exist, return an empty list.

Examples

Example 1

Input

[10, 20, 12, 15, 18, 12, 15, 18, 12, 15, 18, 12]

Output

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Explanation: Step-by-step: Given the input [10, 20, 12, 15, 18, 12, 15, 18, 12, 15, 18, 12], we first initialize an empty list to store the indices of temperature readings that exceed their immediate neighbors. Then, we iterate through the input array. For each element, we check if it exceeds its immediate neighbor. If it does, we append its index to the list. Finally, we return the list. The output [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] indicates that the temperature readings at indices 1, 2, 3, 4, 5, 6, 7, 8, 9, and 10 all exceed their immediate neighbors.

Example 2

Input

[10, 10, 10, 10, 10, 10, 10, 10, 10, 10]

Output

[]

Explanation: Step-by-step: Given the input [10, 10, 10, 10, 10, 10, 10, 10, 10, 10], we first initialize an empty list to store the indices of temperature readings that exceed their immediate neighbors. Then, we iterate through the input array. For each element, we check if it exceeds its immediate neighbor. Since all elements are equal, none of them exceed their immediate neighbors. Therefore, we return an empty list.

Constraints

  • 1 <= array length <= 1000
  • -10000 <= temperature reading <= 10000

Optimal Approach & Strategy

The optimal approach involves iterating through the array and checking each element with its adjacent elements, resulting in a linear time complexity of O(n). This approach is more efficient and suitable for large arrays.

Brute Force Approach

The brute-force approach involves comparing each element with all other elements, resulting in a time complexity of O(n²). This approach is inefficient and not recommended for large arrays.

Verified Code Solutions

JavaScript Solution
Time: O(n)
function peakTemperatureIndices(temperatures) {
  let result = [];
  for (let i = 0; i < temperatures.length - 1; i++) {
    if (i === 0 && temperatures[i] > temperatures[i + 1]) {
      result.push(i);
    } else if (i === temperatures.length - 2 && temperatures[i] > temperatures[i - 1]) {
      result.push(i);
    } else if (temperatures[i] > temperatures[i - 1] && temperatures[i] > temperatures[i + 1]) {
      result.push(i);
    }
  }
  return result;
}

Asked in Top Tech Interviews

PhonePeZomato

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.